DEADSOFTWARE

render: use only r_render to access render
[d2df-sdl.git] / src / game / g_player.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 {$M+}
17 unit g_player;
19 interface
21 uses
22 SysUtils, Classes,
23 {$IFDEF USE_MEMPOOL}mempool,{$ENDIF}
24 g_base, g_playermodel, g_basic, g_textures,
25 g_weapons, g_phys, g_sound, g_saveload, MAPDEF,
26 g_panel;
28 const
29 KEY_LEFT = 1;
30 KEY_RIGHT = 2;
31 KEY_UP = 3;
32 KEY_DOWN = 4;
33 KEY_FIRE = 5;
34 KEY_NEXTWEAPON = 6;
35 KEY_PREVWEAPON = 7;
36 KEY_OPEN = 8;
37 KEY_JUMP = 9;
38 KEY_CHAT = 10;
40 R_ITEM_BACKPACK = 0;
41 R_KEY_RED = 1;
42 R_KEY_GREEN = 2;
43 R_KEY_BLUE = 3;
44 R_BERSERK = 4;
46 MR_SUIT = 0;
47 MR_INVUL = 1;
48 MR_INVIS = 2;
49 MR_MAX = 2;
51 A_BULLETS = 0;
52 A_SHELLS = 1;
53 A_ROCKETS = 2;
54 A_CELLS = 3;
55 A_FUEL = 4;
56 A_HIGH = 4;
58 AmmoLimits: Array [0..1] of Array [A_BULLETS..A_HIGH] of Word =
59 ((200, 50, 50, 300, 100),
60 (400, 100, 100, 600, 200));
62 K_SIMPLEKILL = 0;
63 K_HARDKILL = 1;
64 K_EXTRAHARDKILL = 2;
65 K_FALLKILL = 3;
67 T_RESPAWN = 0;
68 T_SWITCH = 1;
69 T_USE = 2;
70 T_FLAGCAP = 3;
72 TEAM_NONE = 0;
73 TEAM_RED = 1;
74 TEAM_BLUE = 2;
75 TEAM_COOP = 3;
77 SHELL_BULLET = 0;
78 SHELL_SHELL = 1;
79 SHELL_DBLSHELL = 2;
81 ANGLE_NONE = Low(SmallInt);
83 CORPSE_STATE_REMOVEME = 0;
84 CORPSE_STATE_NORMAL = 1;
85 CORPSE_STATE_MESS = 2;
87 PLAYER_RECT: TRectWH = (X:15; Y:12; Width:34; Height:52);
88 PLAYER_RECT_CX = 15+(34 div 2);
89 PLAYER_RECT_CY = 12+(52 div 2);
90 PLAYER_CORPSERECT: TRectWH = (X:15; Y:48; Width:34; Height:16);
92 PLAYER_HP_SOFT = 100;
93 PLAYER_HP_LIMIT = 200;
94 PLAYER_AP_SOFT = 100;
95 PLAYER_AP_LIMIT = 200;
96 SUICIDE_DAMAGE = 112;
97 WEAPON_DELAY = 5;
99 PLAYER_BURN_TIME = 110;
101 PLAYER1_DEF_COLOR: TRGB = (R:64; G:175; B:48);
102 PLAYER2_DEF_COLOR: TRGB = (R:96; G:96; B:96);
104 AIR_DEF = 360;
105 AIR_MAX = 1091;
106 JET_MAX = 540; // ~30 sec
107 ANGLE_RIGHTUP = 55;
108 ANGLE_RIGHTDOWN = -35;
109 ANGLE_LEFTUP = 125;
110 ANGLE_LEFTDOWN = -145;
111 WEAPONPOINT: Array [TDirection] of TDFPoint = ((X:16; Y:32), (X:47; Y:32));
112 TEAMCOLOR: Array [TEAM_RED..TEAM_BLUE] of TRGB = ((R:255; G:0; B:0),
113 (R:0; G:0; B:255));
115 type
116 TPlayerStat = record
117 Num: Integer;
118 Ping: Word;
119 Loss: Byte;
120 Name: String;
121 Team: Byte;
122 Frags: SmallInt;
123 Deaths: SmallInt;
124 Lives: Byte;
125 Kills: Word;
126 Color: TRGB;
127 Spectator: Boolean;
128 UID: Word;
129 end;
131 TPlayerStatArray = Array of TPlayerStat;
133 TPlayerSavedState = record
134 Health: Integer;
135 Armor: Integer;
136 Air: Integer;
137 JetFuel: Integer;
138 CurrWeap: Byte;
139 NextWeap: WORD;
140 NextWeapDelay: Byte;
141 Ammo: Array [A_BULLETS..A_HIGH] of Word;
142 MaxAmmo: Array [A_BULLETS..A_HIGH] of Word;
143 Weapon: Array [WP_FIRST..WP_LAST] of Boolean;
144 Rulez: Set of R_ITEM_BACKPACK..R_BERSERK;
145 Used: Boolean;
146 end;
148 TKeyState = record
149 Pressed: Boolean;
150 Time: Word;
151 end;
153 TPlayer = class{$IFDEF USE_MEMPOOL}(TPoolObject){$ENDIF}
154 private
155 FIamBot: Boolean;
156 FUID: Word;
157 FName: String;
158 FTeam: Byte;
159 FAlive: Boolean;
160 FSpawned: Boolean;
161 FDirection: TDirection;
162 FHealth: Integer;
163 FLives: Byte;
164 FArmor: Integer;
165 FAir: Integer;
166 FPain: Integer;
167 FPickup: Integer;
168 FKills: Integer;
169 FMonsterKills: Integer;
170 FFrags: Integer;
171 FFragCombo: Byte;
172 FLastFrag: LongWord;
173 FComboEvnt: Integer;
174 FDeath: Integer;
175 FCanJetpack: Boolean;
176 FJetFuel: Integer;
177 FFlag: Byte;
178 FSecrets: Integer;
179 FCurrWeap: Byte;
180 FNextWeap: WORD;
181 FNextWeapDelay: Byte; // frames
182 FBFGFireCounter: SmallInt;
183 FLastSpawnerUID: Word;
184 FLastHit: Byte;
185 FObj: TObj;
186 FXTo, FYTo: Integer;
187 FSpectatePlayer: Integer;
188 FFirePainTime: Integer;
189 FFireAttacker: Word;
191 FSavedStateNum: Integer;
193 FModel: TPlayerModel;
194 FPunchAnim: TAnimationState;
195 FActionPrior: Byte;
196 FActionAnim: Byte;
197 FActionForce: Boolean;
198 FActionChanged: Boolean;
199 FAngle: SmallInt;
200 FFireAngle: SmallInt;
201 FIncCamOld: Integer;
202 FIncCam: Integer;
203 FSlopeOld: Integer;
204 FShellTimer: Integer;
205 FShellType: Byte;
206 FSawSound: TPlayableSound;
207 FSawSoundIdle: TPlayableSound;
208 FSawSoundHit: TPlayableSound;
209 FSawSoundSelect: TPlayableSound;
210 FFlameSoundOn: TPlayableSound;
211 FFlameSoundOff: TPlayableSound;
212 FFlameSoundWork: TPlayableSound;
213 FJetSoundOn: TPlayableSound;
214 FJetSoundOff: TPlayableSound;
215 FJetSoundFly: TPlayableSound;
216 FGodMode: Boolean;
217 FNoTarget: Boolean;
218 FNoReload: Boolean;
219 FJustTeleported: Boolean;
220 FNetTime: LongWord;
221 mEDamageType: Integer;
224 function CollideLevel(XInc, YInc: Integer): Boolean;
225 function StayOnStep(XInc, YInc: Integer): Boolean;
226 function HeadInLiquid(XInc, YInc: Integer): Boolean;
227 function BodyInLiquid(XInc, YInc: Integer): Boolean;
228 function BodyInAcid(XInc, YInc: Integer): Boolean;
229 function FullInLift(XInc, YInc: Integer): Integer;
230 {procedure CollideItem();}
231 procedure FlySmoke(Times: DWORD = 1);
232 procedure OnFireFlame(Times: DWORD = 1);
233 procedure SetAction(Action: Byte; Force: Boolean = False);
234 procedure OnDamage(Angle: SmallInt); virtual;
235 function firediry(): Integer;
236 procedure DoPunch();
238 procedure Run(Direction: TDirection);
239 procedure NextWeapon();
240 procedure PrevWeapon();
241 procedure SeeUp();
242 procedure SeeDown();
243 procedure Fire();
244 procedure Jump();
245 procedure Use();
247 function getNextWeaponIndex (): Byte; // return 255 for "no switch"
248 procedure resetWeaponQueue ();
249 function hasAmmoForWeapon (weapon: Byte): Boolean;
251 procedure doDamage (v: Integer);
253 function refreshCorpse(): Boolean;
255 public
256 FDamageBuffer: Integer;
258 FAmmo: Array [A_BULLETS..A_HIGH] of Word;
259 FMaxAmmo: Array [A_BULLETS..A_HIGH] of Word;
260 FWeapon: Array [WP_FIRST..WP_LAST] of Boolean;
261 FRulez: Set of R_ITEM_BACKPACK..R_BERSERK;
262 FBerserk: Integer;
263 FMegaRulez: Array [MR_SUIT..MR_MAX] of DWORD;
264 FReloading: Array [WP_FIRST..WP_LAST] of Word;
265 FTime: Array [T_RESPAWN..T_FLAGCAP] of DWORD;
266 FKeys: Array [KEY_LEFT..KEY_CHAT] of TKeyState;
267 FColor: TRGB;
268 FPreferredTeam: Byte;
269 FSpectator: Boolean;
270 FNoRespawn: Boolean;
271 FWantsInGame: Boolean;
272 FGhost: Boolean;
273 FPhysics: Boolean;
274 FFlaming: Boolean;
275 FJetpack: Boolean;
276 FActualModelName: string;
277 FClientID: SmallInt;
278 FPing: Word;
279 FLoss: Byte;
280 FReady: Boolean;
281 FDummy: Boolean;
282 FFireTime: Integer;
283 FSpawnInvul: Integer;
284 FHandicap: Integer;
285 FWaitForFirstSpawn: Boolean; // set to `true` in server, used to spawn a player on first full state request
286 FCorpse: Integer;
288 // debug: viewport offset
289 viewPortX, viewPortY, viewPortW, viewPortH: Integer;
291 function isValidViewPort (): Boolean; inline;
293 constructor Create(); virtual;
294 destructor Destroy(); override;
295 procedure Respawn(Silent: Boolean; Force: Boolean = False); virtual;
296 function GetRespawnPoint(): Byte;
297 procedure PressKey(Key: Byte; Time: Word = 1);
298 procedure ReleaseKeys();
299 procedure SetModel(ModelName: String);
300 procedure SetColor(Color: TRGB);
301 function GetColor(): TRGB;
302 procedure SetWeapon(W: Byte);
303 function IsKeyPressed(K: Byte): Boolean;
304 function GetKeys(): Byte;
305 function PickItem(ItemType: Byte; arespawn: Boolean; var remove: Boolean): Boolean; virtual;
306 function Collide(X, Y: Integer; Width, Height: Word): Boolean; overload;
307 function Collide(Panel: TPanel): Boolean; overload;
308 function Collide(X, Y: Integer): Boolean; overload;
309 procedure SetDirection(Direction: TDirection);
310 procedure GetSecret();
311 function TeleportTo(X, Y: Integer; silent: Boolean; dir: Byte): Boolean;
312 procedure Touch();
313 procedure Push(vx, vy: Integer);
314 procedure ChangeModel(ModelName: String);
315 procedure SwitchTeam;
316 procedure ChangeTeam(Team: Byte);
317 procedure BFGHit();
318 function GetFlag(Flag: Byte): Boolean;
319 procedure SetFlag(Flag: Byte);
320 function DropFlag(Silent: Boolean = True): Boolean;
321 procedure AllRulez(Health: Boolean);
322 procedure RestoreHealthArmor();
323 procedure FragCombo();
324 procedure GiveItem(ItemType: Byte);
325 procedure Damage(value: Word; SpawnerUID: Word; vx, vy: Integer; t: Byte); virtual;
326 function Heal(value: Word; Soft: Boolean): Boolean; virtual;
327 procedure MakeBloodVector(Count: Word; VelX, VelY: Integer);
328 procedure MakeBloodSimple(Count: Word);
329 procedure Kill(KillType: Byte; SpawnerUID: Word; t: Byte);
330 procedure Reset(Force: Boolean);
331 procedure Spectate(NoMove: Boolean = False);
332 procedure SwitchNoClip;
333 procedure SoftReset();
334 procedure PreUpdate();
335 procedure Update(); virtual;
336 procedure RememberState();
337 procedure RecallState();
338 procedure SaveState (st: TStream); virtual;
339 procedure LoadState (st: TStream); virtual;
340 procedure PauseSounds(Enable: Boolean);
341 procedure NetFire(Wpn: Byte; X, Y, AX, AY: Integer; WID: Integer = -1);
342 procedure DoLerp(Level: Integer = 2);
343 procedure SetLerp(XTo, YTo: Integer);
344 procedure QueueWeaponSwitch(Weapon: Byte);
345 procedure RealizeCurrentWeapon();
346 procedure FlamerOn;
347 procedure FlamerOff;
348 procedure JetpackOn;
349 procedure JetpackOff;
350 procedure CatchFire(Attacker: Word; Timeout: Integer = PLAYER_BURN_TIME);
352 //WARNING! this does nothing for now, but still call it!
353 procedure positionChanged (); //WARNING! call this after entity position was changed, or coldet will not work right!
355 procedure getMapBox (out x, y, w, h: Integer); inline;
356 procedure moveBy (dx, dy: Integer); inline;
358 function getCameraObj(): TObj;
360 function GetAmmoByWeapon(Weapon: Byte): Word; // private state
362 public
363 property Vel: TPoint2i read FObj.Vel;
364 property Obj: TObj read FObj;
366 property Name: String read FName write FName;
367 property Model: TPlayerModel read FModel;
368 property Health: Integer read FHealth write FHealth;
369 property Lives: Byte read FLives write FLives;
370 property Armor: Integer read FArmor write FArmor;
371 property Air: Integer read FAir write FAir;
372 property JetFuel: Integer read FJetFuel write FJetFuel;
373 property Frags: Integer read FFrags write FFrags;
374 property Death: Integer read FDeath write FDeath;
375 property Kills: Integer read FKills write FKills;
376 property CurrWeap: Byte read FCurrWeap write FCurrWeap;
377 property MonsterKills: Integer read FMonsterKills write FMonsterKills;
378 property Secrets: Integer read FSecrets;
379 property GodMode: Boolean read FGodMode write FGodMode;
380 property NoTarget: Boolean read FNoTarget write FNoTarget;
381 property NoReload: Boolean read FNoReload write FNoReload;
382 property alive: Boolean read FAlive write FAlive;
383 property Flag: Byte read FFlag;
384 property Team: Byte read FTeam write FTeam;
385 property Direction: TDirection read FDirection;
386 property GameX: Integer read FObj.X write FObj.X;
387 property GameY: Integer read FObj.Y write FObj.Y;
388 property GameVelX: Integer read FObj.Vel.X write FObj.Vel.X;
389 property GameVelY: Integer read FObj.Vel.Y write FObj.Vel.Y;
390 property GameAccelX: Integer read FObj.Accel.X write FObj.Accel.X;
391 property GameAccelY: Integer read FObj.Accel.Y write FObj.Accel.Y;
392 property IncCam: Integer read FIncCam write FIncCam;
393 property IncCamOld: Integer read FIncCamOld write FIncCamOld;
394 property SlopeOld: Integer read FSlopeOld write FSlopeOld;
395 property UID: Word read FUID write FUID;
396 property JustTeleported: Boolean read FJustTeleported write FJustTeleported;
397 property NetTime: LongWord read FNetTime write FNetTime;
399 (* internal state *)
400 property Angle_: SmallInt read FAngle;
401 property Spectator: Boolean read FSpectator;
402 property NoRespawn: Boolean read FNoRespawn;
403 property Berserk: Integer read FBerserk;
404 property Pain: Integer read FPain;
405 property Pickup: Integer read FPickup;
406 property PunchAnim: TAnimationState read FPunchAnim write FPunchAnim;
407 property SpawnInvul: Integer read FSpawnInvul;
408 property Ghost: Boolean read FGhost;
410 published
411 property eName: String read FName write FName;
412 property eHealth: Integer read FHealth write FHealth;
413 property eLives: Byte read FLives write FLives;
414 property eArmor: Integer read FArmor write FArmor;
415 property eAir: Integer read FAir write FAir;
416 property eJetFuel: Integer read FJetFuel write FJetFuel;
417 property eFrags: Integer read FFrags write FFrags;
418 property eDeath: Integer read FDeath write FDeath;
419 property eKills: Integer read FKills write FKills;
420 property eCurrWeap: Byte read FCurrWeap write FCurrWeap;
421 property eMonsterKills: Integer read FMonsterKills write FMonsterKills;
422 property eSecrets: Integer read FSecrets write FSecrets;
423 property eGodMode: Boolean read FGodMode write FGodMode;
424 property eNoTarget: Boolean read FNoTarget write FNoTarget;
425 property eNoReload: Boolean read FNoReload write FNoReload;
426 property eAlive: Boolean read FAlive write FAlive;
427 property eFlag: Byte read FFlag;
428 property eTeam: Byte read FTeam write FTeam;
429 property eDirection: TDirection read FDirection;
430 property eGameX: Integer read FObj.X write FObj.X;
431 property eGameY: Integer read FObj.Y write FObj.Y;
432 property eGameVelX: Integer read FObj.Vel.X write FObj.Vel.X;
433 property eGameVelY: Integer read FObj.Vel.Y write FObj.Vel.Y;
434 property eGameAccelX: Integer read FObj.Accel.X write FObj.Accel.X;
435 property eGameAccelY: Integer read FObj.Accel.Y write FObj.Accel.Y;
436 property eIncCam: Integer read FIncCam write FIncCam;
437 property eUID: Word read FUID;
438 property eJustTeleported: Boolean read FJustTeleported;
439 property eNetTime: LongWord read FNetTime;
441 // set this before assigning something to `eDamage`
442 property eDamageType: Integer read mEDamageType write mEDamageType;
443 property eDamage: Integer write doDamage;
444 end;
446 TDifficult = record
447 public
448 DiagFire: Byte;
449 InvisFire: Byte;
450 DiagPrecision: Byte;
451 FlyPrecision: Byte;
452 Cover: Byte;
453 CloseJump: Byte;
454 WeaponPrior: packed array [WP_FIRST..WP_LAST] of Byte;
455 CloseWeaponPrior: packed array [WP_FIRST..WP_LAST] of Byte;
456 //SafeWeaponPrior: Array [WP_FIRST..WP_LAST] of Byte;
458 public
459 procedure save (st: TStream);
460 procedure load (st: TStream);
461 end;
463 TAIFlag = record
464 Name: String;
465 Value: String;
466 end;
468 TBot = class(TPlayer)
469 private
470 FSelectedWeapon: Byte;
471 FTargetUID: Word;
472 FLastVisible: DWORD;
473 FAIFlags: Array of TAIFlag;
474 FDifficult: TDifficult;
476 function GetRnd(a: Byte): Boolean;
477 function GetInterval(a: Byte; radius: SmallInt): SmallInt;
478 function RunDirection(): TDirection;
479 function FullInStep(XInc, YInc: Integer): Boolean;
480 //function NeedItem(Item: Byte): Byte;
481 procedure SelectWeapon(Dist: Integer);
482 procedure SetAIFlag(aName, fValue: String20);
483 function GetAIFlag(aName: String20): String20;
484 procedure RemoveAIFlag(aName: String20);
485 function Healthy(): Byte;
486 procedure UpdateMove();
487 procedure UpdateCombat();
488 function KeyPressed(Key: Word): Boolean;
489 procedure ReleaseKey(Key: Byte);
490 function TargetOnScreen(TX, TY: Integer): Boolean;
491 procedure OnDamage(Angle: SmallInt); override;
493 public
494 procedure Respawn(Silent: Boolean; Force: Boolean = False); override;
495 constructor Create(); override;
496 destructor Destroy(); override;
497 function PickItem(ItemType: Byte; force: Boolean; var remove: Boolean): Boolean; override;
498 function Heal(value: Word; Soft: Boolean): Boolean; override;
499 procedure Update(); override;
500 procedure SaveState (st: TStream); override;
501 procedure LoadState (st: TStream); override;
502 end;
504 PGib = ^TGib;
505 TGib = record
506 alive: Boolean;
507 RAngle: Integer;
508 Color: TRGB;
509 Obj: TObj;
511 ModelID: Integer;
512 GibID: Integer;
514 procedure getMapBox (out x, y, w, h: Integer); inline;
515 procedure moveBy (dx, dy: Integer); inline;
517 procedure positionChanged (); inline; //WARNING! call this after entity position was changed, or coldet will not work right!
518 end;
521 PShell = ^TShell;
522 TShell = record
523 alive: Boolean;
524 SType: Byte;
525 RAngle: Integer;
526 Timeout: Cardinal;
527 Obj: TObj;
529 procedure getMapBox (out x, y, w, h: Integer); inline;
530 procedure moveBy (dx, dy: Integer); inline;
532 procedure positionChanged (); inline; //WARNING! call this after entity position was changed, or coldet will not work right!
533 end;
535 TCorpse = class{$IFDEF USE_MEMPOOL}(TPoolObject){$ENDIF}
536 private
537 FMess: Boolean;
538 FState: Byte;
539 FDamage: Byte;
540 FObj: TObj;
541 FPlayerUID: Word;
542 FModel: TPlayerModel;
544 public
545 constructor Create(X, Y: Integer; ModelName: String; aMess: Boolean);
546 destructor Destroy(); override;
547 procedure Damage(Value: Word; SpawnerUID: Word; vx, vy: Integer);
548 procedure Update();
549 procedure SaveState (st: TStream);
550 procedure LoadState (st: TStream);
552 procedure getMapBox (out x, y, w, h: Integer); inline;
553 procedure moveBy (dx, dy: Integer); inline;
555 procedure positionChanged (); inline; //WARNING! call this after entity position was changed, or coldet will not work right!
557 function ObjPtr (): PObj; inline;
559 property Obj: TObj read FObj; // copies object
560 property State: Byte read FState;
561 property Mess: Boolean read FMess;
562 property Model: TPlayerModel read FModel;
563 end;
565 TTeamStat = Array [TEAM_RED..TEAM_BLUE] of
566 record
567 Goals: SmallInt;
568 end;
570 var
571 gPlayers: Array of TPlayer;
572 gCorpses: Array of TCorpse;
573 gGibs: Array of TGib;
574 gShells: Array of TShell;
575 gTeamStat: TTeamStat;
576 gFly: Boolean = False;
577 gAimLine: Boolean = False;
578 gChatBubble: Integer = 0;
579 gPlayerIndicator: Integer = 1;
580 gPlayerIndicatorStyle: Integer = 0;
581 gNumBots: Word = 0;
582 gSpectLatchPID1: Word = 0;
583 gSpectLatchPID2: Word = 0;
584 MAX_RUNVEL: Integer = 8;
585 VEL_JUMP: Integer = 10;
586 SHELL_TIMEOUT: Cardinal = 60000;
588 function Lerp(X, Y, Factor: Integer): Integer;
590 procedure g_Gibs_SetMax(Count: Word);
591 function g_Gibs_GetMax(): Word;
592 procedure g_Corpses_SetMax(Count: Word);
593 function g_Corpses_GetMax(): Word;
594 procedure g_Shells_SetMax(Count: Word);
595 function g_Shells_GetMax(): Word;
597 procedure g_Player_Init();
598 procedure g_Player_Free();
599 function g_Player_Create(ModelName: String; Color: TRGB; Team: Byte; Bot: Boolean): Word;
600 function g_Player_CreateFromState (st: TStream): Word;
601 procedure g_Player_Remove(UID: Word);
602 procedure g_Player_ResetTeams();
603 procedure g_Player_PreUpdate();
604 procedure g_Player_UpdateAll();
605 procedure g_Player_RememberAll();
606 procedure g_Player_ResetAll(Force, Silent: Boolean);
607 function g_Player_Get(UID: Word): TPlayer;
608 function g_Player_GetCount(): Byte;
609 function g_Player_GetStats(): TPlayerStatArray;
610 function g_Player_ValidName(Name: String): Boolean;
611 function g_Player_CreateCorpse(Player: TPlayer): Integer;
612 procedure g_Player_CreateGibs (fX, fY, mid: Integer; fColor: TRGB);
613 procedure g_Player_CreateShell(fX, fY, dX, dY: Integer; T: Byte);
614 procedure g_Player_UpdatePhysicalObjects();
615 procedure g_Player_RemoveAllCorpses();
616 procedure g_Player_Corpses_SaveState (st: TStream);
617 procedure g_Player_Corpses_LoadState (st: TStream);
618 procedure g_Player_ResetReady();
619 procedure g_Bot_Add(Team, Difficult: Byte; Handicap: Integer = 100);
620 procedure g_Bot_AddList(Team: Byte; lname: ShortString; num: Integer = -1; Handicap: Integer = 100);
621 procedure g_Bot_MixNames();
622 procedure g_Bot_RemoveAll();
624 implementation
626 uses
627 {$IFDEF ENABLE_HOLMES}
628 g_holmes,
629 {$ENDIF}
630 {$IFNDEF HEADLESS}
631 r_render,
632 {$ENDIF}
633 e_log, g_map, g_items, g_console, g_gfx, Math,
634 g_options, g_triggers, g_menu, g_game, g_grid, e_res,
635 wadreader, g_monsters, CONFIG, g_language,
636 g_net, g_netmsg,
637 utils, xstreams;
639 const PLR_SAVE_VERSION = 0;
641 type
642 TBotProfile = record
643 name: ShortString;
644 model: ShortString;
645 team: Byte;
646 color: TRGB;
647 diag_fire: Byte;
648 invis_fire: Byte;
649 diag_precision: Byte;
650 fly_precision: Byte;
651 cover: Byte;
652 close_jump: Byte;
653 w_prior1: Array [WP_FIRST..WP_LAST] of Byte;
654 w_prior2: Array [WP_FIRST..WP_LAST] of Byte;
655 w_prior3: Array [WP_FIRST..WP_LAST] of Byte;
656 end;
658 const
659 TIME_RESPAWN1 = 1500;
660 TIME_RESPAWN2 = 2000;
661 TIME_RESPAWN3 = 3000;
662 PLAYER_SUIT_TIME = 30000;
663 PLAYER_INVUL_TIME = 30000;
664 PLAYER_INVIS_TIME = 35000;
665 FRAG_COMBO_TIME = 3000;
666 VEL_SW = 4;
667 VEL_FLY = 6;
668 PLAYER_HEADRECT: TRectWH = (X:24; Y:12; Width:20; Height:12);
669 BOT_MAXJUMP = 84;
670 BOT_LONGDIST = 300;
671 BOT_UNSAFEDIST = 128;
672 DIFFICULT_EASY: TDifficult = (DiagFire: 32; InvisFire: 32; DiagPrecision: 32;
673 FlyPrecision: 32; Cover: 32; CloseJump: 32;
674 WeaponPrior:(0,0,0,0,0,0,0,0,0,0,0); CloseWeaponPrior:(0,0,0,0,0,0,0,0,0,0,0));
675 DIFFICULT_MEDIUM: TDifficult = (DiagFire: 127; InvisFire: 127; DiagPrecision: 127;
676 FlyPrecision: 127; Cover: 127; CloseJump: 127;
677 WeaponPrior:(0,0,0,0,0,0,0,0,0,0,0); CloseWeaponPrior:(0,0,0,0,0,0,0,0,0,0,0));
678 DIFFICULT_HARD: TDifficult = (DiagFire: 255; InvisFire: 255; DiagPrecision: 255;
679 FlyPrecision: 255; Cover: 255; CloseJump: 255;
680 WeaponPrior:(0,0,0,0,0,0,0,0,0,0,0); CloseWeaponPrior:(0,0,0,0,0,0,0,0,0,0,0));
681 WEAPON_PRIOR1: Array [WP_FIRST..WP_LAST] of Byte =
682 (WEAPON_FLAMETHROWER, WEAPON_SUPERPULEMET,
683 WEAPON_SHOTGUN2, WEAPON_SHOTGUN1,
684 WEAPON_CHAINGUN, WEAPON_PLASMA, WEAPON_ROCKETLAUNCHER,
685 WEAPON_BFG, WEAPON_PISTOL, WEAPON_SAW, WEAPON_KASTET);
686 WEAPON_PRIOR2: Array [WP_FIRST..WP_LAST] of Byte =
687 (WEAPON_FLAMETHROWER, WEAPON_SUPERPULEMET,
688 WEAPON_BFG, WEAPON_ROCKETLAUNCHER,
689 WEAPON_SHOTGUN2, WEAPON_PLASMA, WEAPON_SHOTGUN1,
690 WEAPON_CHAINGUN, WEAPON_PISTOL, WEAPON_SAW, WEAPON_KASTET);
691 //WEAPON_PRIOR3: Array [WP_FIRST..WP_LAST] of Byte =
692 // (WEAPON_FLAMETHROWER, WEAPON_SUPERPULEMET,
693 // WEAPON_BFG, WEAPON_PLASMA, WEAPON_SHOTGUN2,
694 // WEAPON_CHAINGUN, WEAPON_SHOTGUN1, WEAPON_SAW,
695 // WEAPON_ROCKETLAUNCHER, WEAPON_PISTOL, WEAPON_KASTET);
696 WEAPON_RELOAD: Array [WP_FIRST..WP_LAST] of Byte =
697 (5, 2, 6, 18, 36, 2, 12, 2, 14, 2, 2);
699 PLAYER_SIGNATURE = $52594C50; // 'PLYR'
700 CORPSE_SIGNATURE = $50524F43; // 'CORP'
702 BOTNAMES_FILENAME = 'botnames.txt';
703 BOTLIST_FILENAME = 'botlist.txt';
705 var
706 MaxGibs: Word = 150;
707 MaxCorpses: Word = 20;
708 MaxShells: Word = 300;
709 CurrentGib: Integer = 0;
710 CurrentShell: Integer = 0;
711 BotNames: Array of String;
712 BotList: Array of TBotProfile;
713 SavedStates: Array of TPlayerSavedState;
716 function Lerp(X, Y, Factor: Integer): Integer;
717 begin
718 Result := X + ((Y - X) div Factor);
719 end;
721 function SameTeam(UID1, UID2: Word): Boolean;
722 begin
723 Result := False;
725 if (UID1 > UID_MAX_PLAYER) or (UID1 <= UID_MAX_GAME) or
726 (UID2 > UID_MAX_PLAYER) or (UID2 <= UID_MAX_GAME) then Exit;
728 if (g_Player_Get(UID1) = nil) or (g_Player_Get(UID2) = nil) then Exit;
730 if ((g_Player_Get(UID1).Team = TEAM_NONE) or
731 (g_Player_Get(UID2).Team = TEAM_NONE)) then Exit;
733 Result := g_Player_Get(UID1).FTeam = g_Player_Get(UID2).FTeam;
734 end;
736 procedure g_Gibs_SetMax(Count: Word);
737 begin
738 MaxGibs := Count;
739 SetLength(gGibs, Count);
741 if CurrentGib >= Count then
742 CurrentGib := 0;
743 end;
745 function g_Gibs_GetMax(): Word;
746 begin
747 Result := MaxGibs;
748 end;
750 procedure g_Shells_SetMax(Count: Word);
751 begin
752 MaxShells := Count;
753 SetLength(gShells, Count);
755 if CurrentShell >= Count then
756 CurrentShell := 0;
757 end;
759 function g_Shells_GetMax(): Word;
760 begin
761 Result := MaxShells;
762 end;
765 procedure g_Corpses_SetMax(Count: Word);
766 begin
767 MaxCorpses := Count;
768 SetLength(gCorpses, Count);
769 end;
771 function g_Corpses_GetMax(): Word;
772 begin
773 Result := MaxCorpses;
774 end;
776 function g_Player_Create(ModelName: String; Color: TRGB; Team: Byte; Bot: Boolean): Word;
777 var
778 a: Integer;
779 ok: Boolean;
780 begin
781 Result := 0;
783 ok := False;
784 a := 0;
786 // Есть ли место в gPlayers:
787 if gPlayers <> nil then
788 for a := 0 to High(gPlayers) do
789 if gPlayers[a] = nil then
790 begin
791 ok := True;
792 Break;
793 end;
795 // Нет места - расширяем gPlayers:
796 if not ok then
797 begin
798 SetLength(gPlayers, Length(gPlayers)+1);
799 a := High(gPlayers);
800 end;
802 // Создаем объект игрока:
803 if Bot then
804 gPlayers[a] := TBot.Create()
805 else
806 gPlayers[a] := TPlayer.Create();
809 gPlayers[a].FActualModelName := ModelName;
810 gPlayers[a].SetModel(ModelName);
812 // Нет модели - создание не возможно:
813 if gPlayers[a].FModel = nil then
814 begin
815 gPlayers[a].Free();
816 gPlayers[a] := nil;
817 g_FatalError(Format(_lc[I_GAME_ERROR_MODEL], [ModelName]));
818 Exit;
819 end;
821 if not (Team in [TEAM_RED, TEAM_BLUE]) then
822 if Random(2) = 0 then
823 Team := TEAM_RED
824 else
825 Team := TEAM_BLUE;
826 gPlayers[a].FPreferredTeam := Team;
828 case gGameSettings.GameMode of
829 GM_DM: gPlayers[a].FTeam := TEAM_NONE;
830 GM_TDM,
831 GM_CTF: gPlayers[a].FTeam := gPlayers[a].FPreferredTeam;
832 GM_SINGLE,
833 GM_COOP: gPlayers[a].FTeam := TEAM_COOP;
834 end;
836 // Если командная игра - красим модель в цвет команды:
837 gPlayers[a].FColor := Color;
838 if gPlayers[a].FTeam in [TEAM_RED, TEAM_BLUE] then
839 gPlayers[a].FModel.Color := TEAMCOLOR[gPlayers[a].FTeam]
840 else
841 gPlayers[a].FModel.Color := Color;
843 gPlayers[a].FUID := g_CreateUID(UID_PLAYER);
844 gPlayers[a].FAlive := False;
846 Result := gPlayers[a].FUID;
847 end;
849 function g_Player_CreateFromState (st: TStream): Word;
850 var
851 a, i: Integer;
852 ok, Bot: Boolean;
853 b: Byte;
854 begin
855 result := 0;
856 if (st = nil) then exit; //???
858 // Сигнатура игрока
859 if not utils.checkSign(st, 'PLYR') then raise XStreamError.Create('invalid player signature');
860 if (utils.readByte(st) <> PLR_SAVE_VERSION) then raise XStreamError.Create('invalid player version');
862 // Бот или человек:
863 Bot := utils.readBool(st);
865 ok := false;
866 a := 0;
868 // Есть ли место в gPlayers:
869 for a := 0 to High(gPlayers) do if (gPlayers[a] = nil) then begin ok := true; break; end;
871 // Нет места - расширяем gPlayers
872 if not ok then
873 begin
874 SetLength(gPlayers, Length(gPlayers)+1);
875 a := High(gPlayers);
876 end;
878 // Создаем объект игрока
879 if Bot then
880 gPlayers[a] := TBot.Create()
881 else
882 gPlayers[a] := TPlayer.Create();
883 gPlayers[a].FIamBot := Bot;
884 gPlayers[a].FPhysics := True;
886 // UID игрока
887 gPlayers[a].FUID := utils.readWord(st);
888 // Имя игрока
889 gPlayers[a].FName := utils.readStr(st);
890 // Команда
891 gPlayers[a].FTeam := utils.readByte(st);
892 gPlayers[a].FPreferredTeam := gPlayers[a].FTeam;
893 // Жив ли
894 gPlayers[a].FAlive := utils.readBool(st);
895 // Израсходовал ли все жизни
896 gPlayers[a].FNoRespawn := utils.readBool(st);
897 // Направление
898 b := utils.readByte(st);
899 if b = 1 then gPlayers[a].FDirection := TDirection.D_LEFT else gPlayers[a].FDirection := TDirection.D_RIGHT; // b = 2
900 // Здоровье
901 gPlayers[a].FHealth := utils.readLongInt(st);
902 // Фора
903 gPlayers[a].FHandicap := utils.readLongInt(st);
904 // Жизни
905 gPlayers[a].FLives := utils.readByte(st);
906 // Броня
907 gPlayers[a].FArmor := utils.readLongInt(st);
908 // Запас воздуха
909 gPlayers[a].FAir := utils.readLongInt(st);
910 // Запас горючего
911 gPlayers[a].FJetFuel := utils.readLongInt(st);
912 // Боль
913 gPlayers[a].FPain := utils.readLongInt(st);
914 // Убил
915 gPlayers[a].FKills := utils.readLongInt(st);
916 // Убил монстров
917 gPlayers[a].FMonsterKills := utils.readLongInt(st);
918 // Фрагов
919 gPlayers[a].FFrags := utils.readLongInt(st);
920 // Фрагов подряд
921 gPlayers[a].FFragCombo := utils.readByte(st);
922 // Время последнего фрага
923 gPlayers[a].FLastFrag := utils.readLongWord(st);
924 // Смертей
925 gPlayers[a].FDeath := utils.readLongInt(st);
926 // Какой флаг несет
927 gPlayers[a].FFlag := utils.readByte(st);
928 // Нашел секретов
929 gPlayers[a].FSecrets := utils.readLongInt(st);
930 // Текущее оружие
931 gPlayers[a].FCurrWeap := utils.readByte(st);
932 // Следующее желаемое оружие
933 gPlayers[a].FNextWeap := utils.readWord(st);
934 // ...и пауза
935 gPlayers[a].FNextWeapDelay := utils.readByte(st);
936 // Время зарядки BFG
937 gPlayers[a].FBFGFireCounter := utils.readSmallInt(st);
938 // Буфер урона
939 gPlayers[a].FDamageBuffer := utils.readLongInt(st);
940 // Последний ударивший
941 gPlayers[a].FLastSpawnerUID := utils.readWord(st);
942 // Тип последнего полученного урона
943 gPlayers[a].FLastHit := utils.readByte(st);
944 // Объект игрока:
945 Obj_LoadState(@gPlayers[a].FObj, st);
946 // Текущее количество патронов
947 for i := A_BULLETS to A_HIGH do gPlayers[a].FAmmo[i] := utils.readWord(st);
948 // Максимальное количество патронов
949 for i := A_BULLETS to A_HIGH do gPlayers[a].FMaxAmmo[i] := utils.readWord(st);
950 // Наличие оружия
951 for i := WP_FIRST to WP_LAST do gPlayers[a].FWeapon[i] := utils.readBool(st);
952 // Время перезарядки оружия
953 for i := WP_FIRST to WP_LAST do gPlayers[a].FReloading[i] := utils.readWord(st);
954 // Наличие рюкзака
955 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_ITEM_BACKPACK);
956 // Наличие красного ключа
957 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_KEY_RED);
958 // Наличие зеленого ключа
959 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_KEY_GREEN);
960 // Наличие синего ключа
961 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_KEY_BLUE);
962 // Наличие берсерка
963 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_BERSERK);
964 // Время действия специальных предметов
965 for i := MR_SUIT to MR_MAX do gPlayers[a].FMegaRulez[i] := utils.readLongWord(st);
966 // Время до повторного респауна, смены оружия, исользования, захвата флага
967 for i := T_RESPAWN to T_FLAGCAP do gPlayers[a].FTime[i] := utils.readLongWord(st);
969 // Название модели:
970 gPlayers[a].FActualModelName := utils.readStr(st);
971 // Цвет модели
972 gPlayers[a].FColor.R := utils.readByte(st);
973 gPlayers[a].FColor.G := utils.readByte(st);
974 gPlayers[a].FColor.B := utils.readByte(st);
975 // Обновляем модель игрока
976 gPlayers[a].SetModel(gPlayers[a].FActualModelName);
978 // Нет модели - создание невозможно
979 if (gPlayers[a].FModel = nil) then
980 begin
981 gPlayers[a].Free();
982 gPlayers[a] := nil;
983 g_FatalError(Format(_lc[I_GAME_ERROR_MODEL], [gPlayers[a].FActualModelName]));
984 exit;
985 end;
987 // Если командная игра - красим модель в цвет команды
988 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
989 gPlayers[a].FModel.Color := TEAMCOLOR[gPlayers[a].FTeam]
990 else
991 gPlayers[a].FModel.Color := gPlayers[a].FColor;
993 result := gPlayers[a].FUID;
994 end;
997 procedure g_Player_ResetTeams();
998 var
999 a: Integer;
1000 begin
1001 if g_Game_IsClient then
1002 Exit;
1003 if gPlayers = nil then
1004 Exit;
1005 for a := Low(gPlayers) to High(gPlayers) do
1006 if gPlayers[a] <> nil then
1007 case gGameSettings.GameMode of
1008 GM_DM:
1009 gPlayers[a].ChangeTeam(TEAM_NONE);
1010 GM_TDM, GM_CTF:
1011 if not (gPlayers[a].Team in [TEAM_RED, TEAM_BLUE]) then
1012 if gPlayers[a].FPreferredTeam in [TEAM_RED, TEAM_BLUE] then
1013 gPlayers[a].ChangeTeam(gPlayers[a].FPreferredTeam)
1014 else
1015 if a mod 2 = 0 then
1016 gPlayers[a].ChangeTeam(TEAM_RED)
1017 else
1018 gPlayers[a].ChangeTeam(TEAM_BLUE);
1019 GM_SINGLE,
1020 GM_COOP:
1021 gPlayers[a].ChangeTeam(TEAM_COOP);
1022 end;
1023 end;
1025 procedure g_Bot_Add(Team, Difficult: Byte; Handicap: Integer = 100);
1026 var
1027 m: SSArray;
1028 _name, _model: String;
1029 a, tr, tb: Integer;
1030 begin
1031 if not g_Game_IsServer then Exit;
1033 // Список названий моделей:
1034 m := g_PlayerModel_GetNames();
1035 if m = nil then
1036 Exit;
1038 // Команда:
1039 if (gGameSettings.GameType = GT_SINGLE) or (gGameSettings.GameMode = GM_COOP) then
1040 Team := TEAM_COOP // COOP
1041 else
1042 if gGameSettings.GameMode = GM_DM then
1043 Team := TEAM_NONE // DM
1044 else
1045 if Team = TEAM_NONE then // CTF / TDM
1046 begin
1047 // Автобаланс команд:
1048 tr := 0;
1049 tb := 0;
1051 for a := 0 to High(gPlayers) do
1052 if gPlayers[a] <> nil then
1053 begin
1054 if gPlayers[a].Team = TEAM_RED then
1055 Inc(tr)
1056 else
1057 if gPlayers[a].Team = TEAM_BLUE then
1058 Inc(tb);
1059 end;
1061 if tr > tb then
1062 Team := TEAM_BLUE
1063 else
1064 if tb > tr then
1065 Team := TEAM_RED
1066 else // tr = tb
1067 if Random(2) = 0 then
1068 Team := TEAM_RED
1069 else
1070 Team := TEAM_BLUE;
1071 end;
1073 // Выбираем боту имя:
1074 _name := '';
1075 if BotNames <> nil then
1076 for a := 0 to High(BotNames) do
1077 if g_Player_ValidName(BotNames[a]) then
1078 begin
1079 _name := BotNames[a];
1080 Break;
1081 end;
1083 // Выбираем случайную модель:
1084 _model := m[Random(Length(m))];
1086 // Создаем бота:
1087 with g_Player_Get(g_Player_Create(_model,
1088 _RGB(Min(Random(9)*32, 255),
1089 Min(Random(9)*32, 255),
1090 Min(Random(9)*32, 255)),
1091 Team, True)) as TBot do
1092 begin
1093 // Если имени нет, делаем его из UID бота
1094 if _name = '' then
1095 Name := Format('DFBOT%.5d', [UID])
1096 else
1097 Name := _name;
1099 case Difficult of
1100 1: FDifficult := DIFFICULT_EASY;
1101 2: FDifficult := DIFFICULT_MEDIUM;
1102 else FDifficult := DIFFICULT_HARD;
1103 end;
1105 for a := WP_FIRST to WP_LAST do
1106 begin
1107 FDifficult.WeaponPrior[a] := WEAPON_PRIOR1[a];
1108 FDifficult.CloseWeaponPrior[a] := WEAPON_PRIOR2[a];
1109 //FDifficult.SafeWeaponPrior[a] := WEAPON_PRIOR3[a];
1110 end;
1112 FHandicap := Handicap;
1114 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [Name]), True);
1116 if g_Game_IsNet then MH_SEND_PlayerCreate(UID);
1117 if g_Game_IsServer and (gGameSettings.MaxLives > 0) then
1118 Spectate();
1119 end;
1120 end;
1122 procedure g_Bot_AddList(Team: Byte; lName: ShortString; num: Integer = -1; Handicap: Integer = 100);
1123 var
1124 m: SSArray;
1125 _name, _model: String;
1126 a: Integer;
1127 begin
1128 if not g_Game_IsServer then Exit;
1130 // Список названий моделей:
1131 m := g_PlayerModel_GetNames();
1132 if m = nil then
1133 Exit;
1135 // Команда:
1136 if (gGameSettings.GameType = GT_SINGLE) or (gGameSettings.GameMode = GM_COOP) then
1137 Team := TEAM_COOP // COOP
1138 else
1139 if gGameSettings.GameMode = GM_DM then
1140 Team := TEAM_NONE // DM
1141 else
1142 if Team = TEAM_NONE then
1143 Team := BotList[num].team; // CTF / TDM
1145 // Выбираем настройки бота из списка по номеру или имени:
1146 lName := AnsiLowerCase(lName);
1147 if (num < 0) or (num > Length(BotList)-1) then
1148 num := -1;
1149 if (num = -1) and (lName <> '') and (BotList <> nil) then
1150 for a := 0 to High(BotList) do
1151 if AnsiLowerCase(BotList[a].name) = lName then
1152 begin
1153 num := a;
1154 Break;
1155 end;
1156 if num = -1 then
1157 Exit;
1159 // Имя бота:
1160 _name := BotList[num].name;
1161 // Занято - выбираем случайное:
1162 if not g_Player_ValidName(_name) then
1163 repeat
1164 _name := Format('DFBOT%.2d', [Random(100)]);
1165 until g_Player_ValidName(_name);
1167 // Модель:
1168 _model := BotList[num].model;
1169 // Нет такой - выбираем случайную:
1170 if not InSArray(_model, m) then
1171 _model := m[Random(Length(m))];
1173 // Создаем бота:
1174 with g_Player_Get(g_Player_Create(_model, BotList[num].color, Team, True)) as TBot do
1175 begin
1176 Name := _name;
1178 FDifficult.DiagFire := BotList[num].diag_fire;
1179 FDifficult.InvisFire := BotList[num].invis_fire;
1180 FDifficult.DiagPrecision := BotList[num].diag_precision;
1181 FDifficult.FlyPrecision := BotList[num].fly_precision;
1182 FDifficult.Cover := BotList[num].cover;
1183 FDifficult.CloseJump := BotList[num].close_jump;
1185 FHandicap := Handicap;
1187 for a := WP_FIRST to WP_LAST do
1188 begin
1189 FDifficult.WeaponPrior[a] := BotList[num].w_prior1[a];
1190 FDifficult.CloseWeaponPrior[a] := BotList[num].w_prior2[a];
1191 //FDifficult.SafeWeaponPrior[a] := BotList[num].w_prior3[a];
1192 end;
1194 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [Name]), True);
1196 if g_Game_IsNet then MH_SEND_PlayerCreate(UID);
1197 end;
1198 end;
1200 procedure g_Bot_RemoveAll();
1201 var
1202 a: Integer;
1203 begin
1204 if not g_Game_IsServer then Exit;
1205 if gPlayers = nil then Exit;
1207 for a := 0 to High(gPlayers) do
1208 if gPlayers[a] <> nil then
1209 if gPlayers[a] is TBot then
1210 begin
1211 gPlayers[a].Lives := 0;
1212 gPlayers[a].Kill(K_SIMPLEKILL, 0, HIT_DISCON);
1213 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [gPlayers[a].Name]), True);
1214 g_Player_Remove(gPlayers[a].FUID);
1215 end;
1217 g_Bot_MixNames();
1218 end;
1220 procedure g_Bot_MixNames();
1221 var
1222 s: String;
1223 a, b: Integer;
1224 begin
1225 if BotNames <> nil then
1226 for a := 0 to High(BotNames) do
1227 begin
1228 b := Random(Length(BotNames));
1229 s := BotNames[a];
1230 Botnames[a] := BotNames[b];
1231 BotNames[b] := s;
1232 end;
1233 end;
1235 procedure g_Player_Remove(UID: Word);
1236 var
1237 i: Integer;
1238 begin
1239 if gPlayers = nil then Exit;
1241 if g_Game_IsServer and g_Game_IsNet then
1242 MH_SEND_PlayerDelete(UID);
1244 for i := 0 to High(gPlayers) do
1245 if gPlayers[i] <> nil then
1246 if gPlayers[i].FUID = UID then
1247 begin
1248 if gPlayers[i] is TPlayer then
1249 TPlayer(gPlayers[i]).Free()
1250 else
1251 TBot(gPlayers[i]).Free();
1252 gPlayers[i] := nil;
1253 Exit;
1254 end;
1255 end;
1257 procedure g_Player_Init();
1258 var
1259 F: TextFile;
1260 s: String;
1261 a, b: Integer;
1262 config: TConfig;
1263 sa: SSArray;
1264 path: AnsiString;
1265 begin
1266 BotNames := nil;
1268 path := BOTNAMES_FILENAME;
1269 if e_FindResource(DataDirs, path) = false then
1270 Exit;
1272 // Читаем возможные имена ботов из файла:
1273 AssignFile(F, path);
1274 Reset(F);
1276 while not EOF(F) do
1277 begin
1278 ReadLn(F, s);
1280 s := Trim(s);
1281 if s = '' then
1282 Continue;
1284 SetLength(BotNames, Length(BotNames)+1);
1285 BotNames[High(BotNames)] := s;
1286 end;
1288 CloseFile(F);
1290 // Перемешиваем их:
1291 g_Bot_MixNames();
1293 // Читаем файл с параметрами ботов:
1294 config := TConfig.CreateFile(path);
1295 BotList := nil;
1296 a := 0;
1298 while config.SectionExists(IntToStr(a)) do
1299 begin
1300 SetLength(BotList, Length(BotList)+1);
1302 with BotList[High(BotList)] do
1303 begin
1304 // Имя бота:
1305 name := config.ReadStr(IntToStr(a), 'name', '');
1306 // Модель:
1307 model := config.ReadStr(IntToStr(a), 'model', '');
1308 // Команда:
1309 if config.ReadStr(IntToStr(a), 'team', 'red') = 'red' then
1310 team := TEAM_RED
1311 else
1312 team := TEAM_BLUE;
1313 // Цвет модели:
1314 sa := parse(config.ReadStr(IntToStr(a), 'color', ''));
1315 color.R := StrToIntDef(sa[0], 0);
1316 color.G := StrToIntDef(sa[1], 0);
1317 color.B := StrToIntDef(sa[2], 0);
1318 // Вероятность стрельбы под углом:
1319 diag_fire := config.ReadInt(IntToStr(a), 'diag_fire', 0);
1320 // Вероятность ответного огня по невидимому сопернику:
1321 invis_fire := config.ReadInt(IntToStr(a), 'invis_fire', 0);
1322 // Точность стрельбы под углом:
1323 diag_precision := config.ReadInt(IntToStr(a), 'diag_precision', 0);
1324 // Точность стрельбы в полете:
1325 fly_precision := config.ReadInt(IntToStr(a), 'fly_precision', 0);
1326 // Точность уклонения от снарядов:
1327 cover := config.ReadInt(IntToStr(a), 'cover', 0);
1328 // Вероятность прыжка при приближении соперника:
1329 close_jump := config.ReadInt(IntToStr(a), 'close_jump', 0);
1330 // Приоритеты оружия для дальнего боя:
1331 sa := parse(config.ReadStr(IntToStr(a), 'w_prior1', ''));
1332 if Length(sa) = 10 then
1333 for b := 0 to 9 do
1334 w_prior1[b] := EnsureRange(StrToInt(sa[b]), 0, 9);
1335 // Приоритеты оружия для ближнего боя:
1336 sa := parse(config.ReadStr(IntToStr(a), 'w_prior2', ''));
1337 if Length(sa) = 10 then
1338 for b := 0 to 9 do
1339 w_prior2[b] := EnsureRange(StrToInt(sa[b]), 0, 9);
1341 {sa := parse(config.ReadStr(IntToStr(a), 'w_prior3', ''));
1342 if Length(sa) = 10 then
1343 for b := 0 to 9 do
1344 w_prior3[b] := EnsureRange(StrToInt(sa[b]), 0, 9);}
1345 end;
1347 a := a + 1;
1348 end;
1350 config.Free();
1351 SetLength(SavedStates, 0);
1352 end;
1354 procedure g_Player_Free();
1355 var
1356 i: Integer;
1357 begin
1358 if gPlayers <> nil then
1359 begin
1360 for i := 0 to High(gPlayers) do
1361 if gPlayers[i] <> nil then
1362 begin
1363 if gPlayers[i] is TPlayer then
1364 TPlayer(gPlayers[i]).Free()
1365 else
1366 TBot(gPlayers[i]).Free();
1367 gPlayers[i] := nil;
1368 end;
1370 gPlayers := nil;
1371 end;
1373 gPlayer1 := nil;
1374 gPlayer2 := nil;
1375 SetLength(SavedStates, 0);
1376 end;
1378 procedure g_Player_PreUpdate();
1379 var
1380 i: Integer;
1381 begin
1382 if gPlayers = nil then Exit;
1383 for i := 0 to High(gPlayers) do
1384 if gPlayers[i] <> nil then
1385 gPlayers[i].PreUpdate();
1386 end;
1388 procedure g_Player_UpdateAll();
1389 var
1390 i: Integer;
1391 begin
1392 if gPlayers = nil then Exit;
1394 //e_WriteLog('***g_Player_UpdateAll: ENTER', MSG_WARNING);
1395 for i := 0 to High(gPlayers) do
1396 begin
1397 if gPlayers[i] <> nil then
1398 begin
1399 if gPlayers[i] is TPlayer then
1400 begin
1401 gPlayers[i].Update();
1402 gPlayers[i].RealizeCurrentWeapon(); // WARNING! DO NOT MOVE THIS INTO `Update()`!
1403 end
1404 else
1405 begin
1406 // bot updates weapons in `UpdateCombat()`
1407 TBot(gPlayers[i]).Update();
1408 end;
1409 end;
1410 end;
1411 //e_WriteLog('***g_Player_UpdateAll: EXIT', MSG_WARNING);
1412 end;
1414 function g_Player_Get(UID: Word): TPlayer;
1415 var
1416 a: Integer;
1417 begin
1418 Result := nil;
1420 if gPlayers = nil then
1421 Exit;
1423 for a := 0 to High(gPlayers) do
1424 if gPlayers[a] <> nil then
1425 if gPlayers[a].FUID = UID then
1426 begin
1427 Result := gPlayers[a];
1428 Exit;
1429 end;
1430 end;
1432 function g_Player_GetCount(): Byte;
1433 var
1434 a: Integer;
1435 begin
1436 Result := 0;
1438 if gPlayers = nil then
1439 Exit;
1441 for a := 0 to High(gPlayers) do
1442 if gPlayers[a] <> nil then
1443 Result := Result + 1;
1444 end;
1446 function g_Player_GetStats(): TPlayerStatArray;
1447 var
1448 a: Integer;
1449 begin
1450 Result := nil;
1452 if gPlayers = nil then Exit;
1454 for a := 0 to High(gPlayers) do
1455 if gPlayers[a] <> nil then
1456 begin
1457 SetLength(Result, Length(Result)+1);
1458 with Result[High(Result)] do
1459 begin
1460 Num := a;
1461 Ping := gPlayers[a].FPing;
1462 Loss := gPlayers[a].FLoss;
1463 Name := gPlayers[a].FName;
1464 Team := gPlayers[a].FTeam;
1465 Frags := gPlayers[a].FFrags;
1466 Deaths := gPlayers[a].FDeath;
1467 Kills := gPlayers[a].FKills;
1468 Color := gPlayers[a].FModel.Color;
1469 Lives := gPlayers[a].FLives;
1470 Spectator := gPlayers[a].FSpectator;
1471 UID := gPlayers[a].FUID;
1472 end;
1473 end;
1474 end;
1476 procedure g_Player_ResetReady();
1477 var
1478 a: Integer;
1479 begin
1480 if not g_Game_IsServer then Exit;
1481 if gPlayers = nil then Exit;
1483 for a := 0 to High(gPlayers) do
1484 if gPlayers[a] <> nil then
1485 begin
1486 gPlayers[a].FReady := False;
1487 if g_Game_IsNet then
1488 MH_SEND_GameEvent(NET_EV_INTER_READY, gPlayers[a].UID, 'N');
1489 end;
1490 end;
1492 procedure g_Player_RememberAll;
1493 var
1494 i: Integer;
1495 begin
1496 for i := Low(gPlayers) to High(gPlayers) do
1497 if (gPlayers[i] <> nil) and gPlayers[i].alive then
1498 gPlayers[i].RememberState;
1499 end;
1501 procedure g_Player_ResetAll(Force, Silent: Boolean);
1502 var
1503 i: Integer;
1504 begin
1505 gTeamStat[TEAM_RED].Goals := 0;
1506 gTeamStat[TEAM_BLUE].Goals := 0;
1508 if gPlayers <> nil then
1509 for i := 0 to High(gPlayers) do
1510 if gPlayers[i] <> nil then
1511 begin
1512 gPlayers[i].Reset(Force);
1514 if gPlayers[i] is TPlayer then
1515 begin
1516 if (not gPlayers[i].FSpectator) or gPlayers[i].FWantsInGame then
1517 gPlayers[i].Respawn(Silent)
1518 else
1519 gPlayers[i].Spectate();
1520 end
1521 else
1522 TBot(gPlayers[i]).Respawn(Silent);
1523 end;
1524 end;
1526 function g_Player_CreateCorpse(Player: TPlayer): Integer;
1527 var
1528 i: Integer;
1529 find_id: DWORD;
1530 ok: Boolean;
1531 begin
1532 Result := -1;
1534 if Player.alive then
1535 Exit;
1537 // Разрываем связь с прежним трупом:
1538 i := Player.FCorpse;
1539 if (i >= 0) and (i < Length(gCorpses)) then
1540 begin
1541 if (gCorpses[i] <> nil) and (gCorpses[i].FPlayerUID = Player.FUID) then
1542 gCorpses[i].FPlayerUID := 0;
1543 end;
1545 if Player.FObj.Y >= gMapInfo.Height+128 then
1546 Exit;
1548 with Player do
1549 begin
1550 if (FHealth >= -50) or (gGibsCount = 0) then
1551 begin
1552 if (gCorpses = nil) or (Length(gCorpses) = 0) then
1553 Exit;
1555 ok := False;
1556 for find_id := 0 to High(gCorpses) do
1557 if gCorpses[find_id] = nil then
1558 begin
1559 ok := True;
1560 Break;
1561 end;
1563 if not ok then
1564 find_id := Random(Length(gCorpses));
1566 gCorpses[find_id] := TCorpse.Create(FObj.X, FObj.Y, FModel.GetName(), FHealth < -20);
1567 gCorpses[find_id].FModel.Color := FModel.Color;
1568 gCorpses[find_id].FObj.Vel := FObj.Vel;
1569 gCorpses[find_id].FObj.Accel := FObj.Accel;
1570 gCorpses[find_id].FPlayerUID := FUID;
1572 Result := find_id;
1573 end
1574 else
1575 g_Player_CreateGibs(FObj.X + PLAYER_RECT_CX, FObj.Y + PLAYER_RECT_CY, FModel.id, FModel.Color);
1576 end;
1577 end;
1579 procedure g_Player_CreateShell(fX, fY, dX, dY: Integer; T: Byte);
1580 begin
1581 if (gShells = nil) or (Length(gShells) = 0) then
1582 Exit;
1584 with gShells[CurrentShell] do
1585 begin
1586 g_Obj_Init(@Obj);
1587 Obj.Rect.X := 0;
1588 Obj.Rect.Y := 0;
1589 if T = SHELL_BULLET then
1590 begin
1591 Obj.Rect.Width := 4;
1592 Obj.Rect.Height := 2;
1593 end
1594 else
1595 begin
1596 Obj.Rect.Width := 7;
1597 Obj.Rect.Height := 3;
1598 end;
1599 SType := T;
1600 alive := True;
1601 Obj.X := fX;
1602 Obj.Y := fY;
1603 g_Obj_Push(@Obj, dX + Random(4)-Random(4), dY-Random(4));
1604 positionChanged(); // this updates spatial accelerators
1605 RAngle := Random(360);
1606 Timeout := gTime + SHELL_TIMEOUT;
1608 if CurrentShell >= High(gShells) then
1609 CurrentShell := 0
1610 else
1611 Inc(CurrentShell);
1612 end;
1613 end;
1615 procedure g_Player_CreateGibs (fX, fY, mid: Integer; fColor: TRGB);
1616 var
1617 a: Integer;
1618 GibsArray: TGibsArray;
1619 Blood: TModelBlood;
1620 begin
1621 if mid = -1 then
1622 Exit;
1623 if (gGibs = nil) or (Length(gGibs) = 0) then
1624 Exit;
1625 if not g_PlayerModel_GetGibs(mid, GibsArray) then
1626 Exit;
1627 Blood := PlayerModelsArray[mid].Blood;
1629 for a := 0 to High(GibsArray) do
1630 with gGibs[CurrentGib] do
1631 begin
1632 ModelID := mid;
1633 GibID := GibsArray[a];
1634 Color := fColor;
1635 alive := True;
1636 g_Obj_Init(@Obj);
1637 {$IFNDEF HEADLESS}
1638 Obj.Rect := r_Render_GetGibRect(ModelID, GibID);
1639 {$ELSE}
1640 Obj.Rect.X := 16;
1641 Obj.Rect.Y := 16;
1642 Obj.Rect.Width := 16;
1643 Obj.Rect.Height := 16;
1644 {$ENDIF}
1645 Obj.X := fX - Obj.Rect.X - (Obj.Rect.Width div 2);
1646 Obj.Y := fY - Obj.Rect.Y - (Obj.Rect.Height div 2);
1647 g_Obj_PushA(@Obj, 25 + Random(10), Random(361));
1648 positionChanged(); // this updates spatial accelerators
1649 RAngle := Random(360);
1651 if gBloodCount > 0 then
1652 g_GFX_Blood(fX, fY, 16*gBloodCount+Random(5*gBloodCount), -16+Random(33), -16+Random(33),
1653 Random(48), Random(48), Blood.R, Blood.G, Blood.B, Blood.Kind);
1655 if CurrentGib >= High(gGibs) then
1656 CurrentGib := 0
1657 else
1658 Inc(CurrentGib);
1659 end;
1660 end;
1662 procedure g_Player_UpdatePhysicalObjects();
1663 var
1664 i: Integer;
1665 vel: TPoint2i;
1666 mr: Word;
1668 procedure ShellSound_Bounce(X, Y: Integer; T: Byte);
1669 var
1670 k: Integer;
1671 begin
1672 k := 1 + Random(2);
1673 if T = SHELL_BULLET then
1674 g_Sound_PlayExAt('SOUND_PLAYER_CASING' + IntToStr(k), X, Y)
1675 else
1676 g_Sound_PlayExAt('SOUND_PLAYER_SHELL' + IntToStr(k), X, Y);
1677 end;
1679 begin
1680 // Куски мяса:
1681 if gGibs <> nil then
1682 for i := 0 to High(gGibs) do
1683 if gGibs[i].alive then
1684 with gGibs[i] do
1685 begin
1686 Obj.oldX := Obj.X;
1687 Obj.oldY := Obj.Y;
1689 vel := Obj.Vel;
1690 mr := g_Obj_Move(@Obj, True, False, True);
1691 positionChanged(); // this updates spatial accelerators
1693 if WordBool(mr and MOVE_FALLOUT) then
1694 begin
1695 alive := False;
1696 Continue;
1697 end;
1699 // Отлетает от удара о стену/потолок/пол:
1700 if WordBool(mr and MOVE_HITWALL) then
1701 Obj.Vel.X := -(vel.X div 2);
1702 if WordBool(mr and (MOVE_HITCEIL or MOVE_HITLAND)) then
1703 Obj.Vel.Y := -(vel.Y div 2);
1705 if (Obj.Vel.X >= 0) then
1706 begin // Clockwise
1707 RAngle := RAngle + Abs(Obj.Vel.X)*6 + Abs(Obj.Vel.Y);
1708 if RAngle >= 360 then
1709 RAngle := RAngle mod 360;
1710 end else begin // Counter-clockwise
1711 RAngle := RAngle - Abs(Obj.Vel.X)*6 - Abs(Obj.Vel.Y);
1712 if RAngle < 0 then
1713 RAngle := (360 - (Abs(RAngle) mod 360)) mod 360;
1714 end;
1716 // Сопротивление воздуха для куска трупа:
1717 if gTime mod (GAME_TICK*3) = 0 then
1718 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1719 end;
1721 // Трупы:
1722 if gCorpses <> nil then
1723 for i := 0 to High(gCorpses) do
1724 if gCorpses[i] <> nil then
1725 if gCorpses[i].State = CORPSE_STATE_REMOVEME then
1726 begin
1727 gCorpses[i].Free();
1728 gCorpses[i] := nil;
1729 end
1730 else
1731 gCorpses[i].Update();
1733 // Гильзы:
1734 if gShells <> nil then
1735 for i := 0 to High(gShells) do
1736 if gShells[i].alive then
1737 with gShells[i] do
1738 begin
1739 Obj.oldX := Obj.X;
1740 Obj.oldY := Obj.Y;
1742 vel := Obj.Vel;
1743 mr := g_Obj_Move(@Obj, True, False, True);
1744 positionChanged(); // this updates spatial accelerators
1746 if WordBool(mr and MOVE_FALLOUT) or (gShells[i].Timeout < gTime) then
1747 begin
1748 alive := False;
1749 Continue;
1750 end;
1752 // Отлетает от удара о стену/потолок/пол:
1753 if WordBool(mr and MOVE_HITWALL) then
1754 begin
1755 Obj.Vel.X := -(vel.X div 2);
1756 if not WordBool(mr and MOVE_INWATER) then
1757 ShellSound_Bounce(Obj.X, Obj.Y, SType);
1758 end;
1759 if WordBool(mr and (MOVE_HITCEIL or MOVE_HITLAND)) then
1760 begin
1761 Obj.Vel.Y := -(vel.Y div 2);
1762 if Obj.Vel.X <> 0 then Obj.Vel.X := Obj.Vel.X div 2;
1763 if (Obj.Vel.X = 0) and (Obj.Vel.Y = 0) then
1764 begin
1765 if RAngle mod 90 <> 0 then
1766 RAngle := (RAngle div 90) * 90;
1767 end
1768 else if not WordBool(mr and MOVE_INWATER) then
1769 ShellSound_Bounce(Obj.X, Obj.Y, SType);
1770 end;
1772 if (Obj.Vel.X >= 0) then
1773 begin // Clockwise
1774 RAngle := RAngle + Abs(Obj.Vel.X)*8 + Abs(Obj.Vel.Y);
1775 if RAngle >= 360 then
1776 RAngle := RAngle mod 360;
1777 end else begin // Counter-clockwise
1778 RAngle := RAngle - Abs(Obj.Vel.X)*8 - Abs(Obj.Vel.Y);
1779 if RAngle < 0 then
1780 RAngle := (360 - (Abs(RAngle) mod 360)) mod 360;
1781 end;
1782 end;
1783 end;
1786 procedure TGib.getMapBox (out x, y, w, h: Integer); inline;
1787 begin
1788 x := Obj.X+Obj.Rect.X;
1789 y := Obj.Y+Obj.Rect.Y;
1790 w := Obj.Rect.Width;
1791 h := Obj.Rect.Height;
1792 end;
1794 procedure TGib.moveBy (dx, dy: Integer); inline;
1795 begin
1796 if (dx <> 0) or (dy <> 0) then
1797 begin
1798 Obj.X += dx;
1799 Obj.Y += dy;
1800 positionChanged();
1801 end;
1802 end;
1805 procedure TShell.getMapBox (out x, y, w, h: Integer); inline;
1806 begin
1807 x := Obj.X;
1808 y := Obj.Y;
1809 w := Obj.Rect.Width;
1810 h := Obj.Rect.Height;
1811 end;
1813 procedure TShell.moveBy (dx, dy: Integer); inline;
1814 begin
1815 if (dx <> 0) or (dy <> 0) then
1816 begin
1817 Obj.X += dx;
1818 Obj.Y += dy;
1819 positionChanged();
1820 end;
1821 end;
1824 procedure TGib.positionChanged (); inline; begin end;
1825 procedure TShell.positionChanged (); inline; begin end;
1828 procedure g_Player_RemoveAllCorpses();
1829 var
1830 i: Integer;
1831 begin
1832 gGibs := nil;
1833 gShells := nil;
1834 SetLength(gGibs, MaxGibs);
1835 SetLength(gShells, MaxGibs);
1836 CurrentGib := 0;
1837 CurrentShell := 0;
1839 if gCorpses <> nil then
1840 for i := 0 to High(gCorpses) do
1841 gCorpses[i].Free();
1843 gCorpses := nil;
1844 SetLength(gCorpses, MaxCorpses);
1845 end;
1847 procedure g_Player_Corpses_SaveState (st: TStream);
1848 var
1849 count, i: Integer;
1850 begin
1851 // Считаем количество существующих трупов
1852 count := 0;
1853 for i := 0 to High(gCorpses) do if (gCorpses[i] <> nil) then Inc(count);
1855 // Количество трупов
1856 utils.writeInt(st, LongInt(count));
1858 if (count = 0) then exit;
1860 // Сохраняем трупы
1861 for i := 0 to High(gCorpses) do
1862 begin
1863 if gCorpses[i] <> nil then
1864 begin
1865 // Название модели
1866 utils.writeStr(st, gCorpses[i].FModel.GetName());
1867 // Тип смерти
1868 utils.writeBool(st, gCorpses[i].Mess);
1869 // Сохраняем данные трупа:
1870 gCorpses[i].SaveState(st);
1871 end;
1872 end;
1873 end;
1876 procedure g_Player_Corpses_LoadState (st: TStream);
1877 var
1878 count, i: Integer;
1879 str: String;
1880 b: Boolean;
1881 begin
1882 assert(st <> nil);
1884 g_Player_RemoveAllCorpses();
1886 // Количество трупов:
1887 count := utils.readLongInt(st);
1888 if (count < 0) or (count > Length(gCorpses)) then raise XStreamError.Create('invalid number of corpses');
1890 if (count = 0) then exit;
1892 // Загружаем трупы
1893 for i := 0 to count-1 do
1894 begin
1895 // Название модели:
1896 str := utils.readStr(st);
1897 // Тип смерти
1898 b := utils.readBool(st);
1899 // Создаем труп
1900 gCorpses[i] := TCorpse.Create(0, 0, str, b);
1901 // Загружаем данные трупа
1902 gCorpses[i].LoadState(st);
1903 end;
1904 end;
1907 { T P l a y e r : }
1909 function TPlayer.isValidViewPort (): Boolean; inline; begin result := (viewPortW > 0) and (viewPortH > 0); end;
1911 procedure TPlayer.BFGHit();
1912 begin
1913 g_Weapon_BFGHit(FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2),
1914 FObj.Y+FObj.Rect.Y+(FObj.Rect.Height div 2));
1915 if g_Game_IsServer and g_Game_IsNet then
1916 MH_SEND_Effect(FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2),
1917 FObj.Y+FObj.Rect.Y+(FObj.Rect.Height div 2),
1918 0, NET_GFX_BFGHIT);
1919 end;
1921 procedure TPlayer.ChangeModel(ModelName: string);
1922 var
1923 locModel: TPlayerModel;
1924 begin
1925 locModel := g_PlayerModel_Get(ModelName);
1926 if locModel = nil then Exit;
1928 FModel.Free();
1929 FModel := locModel;
1930 end;
1932 procedure TPlayer.SetModel(ModelName: string);
1933 var
1934 m: TPlayerModel;
1935 begin
1936 m := g_PlayerModel_Get(ModelName);
1937 if m = nil then
1938 begin
1939 g_SimpleError(Format(_lc[I_GAME_ERROR_MODEL_FALLBACK], [ModelName]));
1940 m := g_PlayerModel_Get('doomer');
1941 if m = nil then
1942 begin
1943 g_FatalError(Format(_lc[I_GAME_ERROR_MODEL], ['doomer']));
1944 Exit;
1945 end;
1946 end;
1948 if FModel <> nil then
1949 FModel.Free();
1951 FModel := m;
1953 if not (gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
1954 FModel.Color := FColor
1955 else
1956 FModel.Color := TEAMCOLOR[FTeam];
1957 FModel.SetWeapon(FCurrWeap);
1958 FModel.SetFlag(FFlag);
1959 SetDirection(FDirection);
1960 end;
1962 procedure TPlayer.SetColor(Color: TRGB);
1963 begin
1964 FColor := Color;
1965 if not (gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
1966 if FModel <> nil then FModel.Color := Color;
1967 end;
1969 function TPlayer.GetColor(): TRGB;
1970 begin
1971 result := FModel.Color;
1972 end;
1974 procedure TPlayer.SwitchTeam;
1975 begin
1976 if g_Game_IsClient then
1977 Exit;
1978 if not (gGameSettings.GameMode in [GM_TDM, GM_CTF]) then Exit;
1980 if gGameOn and FAlive then
1981 Kill(K_SIMPLEKILL, FUID, HIT_SELF);
1983 if FTeam = TEAM_RED then
1984 begin
1985 ChangeTeam(TEAM_BLUE);
1986 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [FName]), True);
1987 if g_Game_IsNet then
1988 MH_SEND_GameEvent(NET_EV_CHANGE_TEAM, TEAM_BLUE, FName);
1989 end
1990 else
1991 begin
1992 ChangeTeam(TEAM_RED);
1993 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [FName]), True);
1994 if g_Game_IsNet then
1995 MH_SEND_GameEvent(NET_EV_CHANGE_TEAM, TEAM_RED, FName);
1996 end;
1997 FPreferredTeam := FTeam;
1998 end;
2000 procedure TPlayer.ChangeTeam(Team: Byte);
2001 var
2002 OldTeam: Byte;
2003 begin
2004 OldTeam := FTeam;
2005 FTeam := Team;
2006 case Team of
2007 TEAM_RED, TEAM_BLUE:
2008 FModel.Color := TEAMCOLOR[Team];
2009 else
2010 FModel.Color := FColor;
2011 end;
2012 if (FTeam <> OldTeam) and g_Game_IsNet and g_Game_IsServer then
2013 MH_SEND_PlayerStats(FUID);
2014 end;
2017 procedure TPlayer.CollideItem();
2018 var
2019 i: Integer;
2020 r: Boolean;
2021 begin
2022 if gItems = nil then Exit;
2023 if not FAlive then Exit;
2025 for i := 0 to High(gItems) do
2026 with gItems[i] do
2027 begin
2028 if (ItemType <> ITEM_NONE) and alive then
2029 if g_Obj_Collide(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y, PLAYER_RECT.Width,
2030 PLAYER_RECT.Height, @Obj) then
2031 begin
2032 if not PickItem(ItemType, gItems[i].Respawnable, r) then Continue;
2034 if ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL] then
2035 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', FObj.X, FObj.Y)
2036 else if ItemType in [ITEM_MEDKIT_SMALL, ITEM_MEDKIT_LARGE, ITEM_MEDKIT_BLACK] then
2037 g_Sound_PlayExAt('SOUND_ITEM_GETMED', FObj.X, FObj.Y)
2038 else g_Sound_PlayExAt('SOUND_ITEM_GETITEM', FObj.X, FObj.Y);
2040 // Надо убрать с карты, если это не ключ, которым нужно поделится с другим игроком:
2041 if r and not ((ItemType in [ITEM_KEY_RED, ITEM_KEY_GREEN, ITEM_KEY_BLUE]) and
2042 (gGameSettings.GameType = GT_SINGLE) and
2043 (g_Player_GetCount() > 1)) then
2044 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
2045 end;
2046 end;
2047 end;
2050 function TPlayer.CollideLevel(XInc, YInc: Integer): Boolean;
2051 begin
2052 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
2053 PLAYER_RECT.Width, PLAYER_RECT.Height, PANEL_WALL,
2054 False);
2055 end;
2057 constructor TPlayer.Create();
2058 begin
2059 viewPortX := 0;
2060 viewPortY := 0;
2061 viewPortW := 0;
2062 viewPortH := 0;
2063 mEDamageType := HIT_SOME;
2065 FIamBot := False;
2066 FDummy := False;
2067 FSpawned := False;
2069 FSawSound := TPlayableSound.Create();
2070 FSawSoundIdle := TPlayableSound.Create();
2071 FSawSoundHit := TPlayableSound.Create();
2072 FSawSoundSelect := TPlayableSound.Create();
2073 FFlameSoundOn := TPlayableSound.Create();
2074 FFlameSoundOff := TPlayableSound.Create();
2075 FFlameSoundWork := TPlayableSound.Create();
2076 FJetSoundFly := TPlayableSound.Create();
2077 FJetSoundOn := TPlayableSound.Create();
2078 FJetSoundOff := TPlayableSound.Create();
2080 FSawSound.SetByName('SOUND_WEAPON_FIRESAW');
2081 FSawSoundIdle.SetByName('SOUND_WEAPON_IDLESAW');
2082 FSawSoundHit.SetByName('SOUND_WEAPON_HITSAW');
2083 FSawSoundSelect.SetByName('SOUND_WEAPON_SELECTSAW');
2084 FFlameSoundOn.SetByName('SOUND_WEAPON_FLAMEON');
2085 FFlameSoundOff.SetByName('SOUND_WEAPON_FLAMEOFF');
2086 FFlameSoundWork.SetByName('SOUND_WEAPON_FLAMEWORK');
2087 FJetSoundFly.SetByName('SOUND_PLAYER_JETFLY');
2088 FJetSoundOn.SetByName('SOUND_PLAYER_JETON');
2089 FJetSoundOff.SetByName('SOUND_PLAYER_JETOFF');
2091 FSpectatePlayer := -1;
2092 FClientID := -1;
2093 FPing := 0;
2094 FLoss := 0;
2095 FSavedStateNum := -1;
2096 FShellTimer := -1;
2097 FFireTime := 0;
2098 FFirePainTime := 0;
2099 FFireAttacker := 0;
2100 FHandicap := 100;
2101 FCorpse := -1;
2103 FActualModelName := 'doomer';
2105 g_Obj_Init(@FObj);
2106 FObj.Rect := PLAYER_RECT;
2108 FBFGFireCounter := -1;
2109 FJustTeleported := False;
2110 FNetTime := 0;
2112 FWaitForFirstSpawn := false;
2113 FPunchAnim := TAnimationState.Create(False, 1, 4);
2114 FPunchAnim.Disable;
2116 resetWeaponQueue();
2117 end;
2119 procedure TPlayer.positionChanged (); inline;
2120 begin
2121 end;
2123 procedure TPlayer.doDamage (v: Integer);
2124 begin
2125 if (v <= 0) then exit;
2126 if (v > 32767) then v := 32767;
2127 Damage(v, 0, 0, 0, mEDamageType);
2128 end;
2130 procedure TPlayer.Damage(value: Word; SpawnerUID: Word; vx, vy: Integer; t: Byte);
2131 var
2132 c: Word;
2133 begin
2134 if (not g_Game_IsClient) and (not FAlive) then
2135 Exit;
2137 FLastHit := t;
2139 // Неуязвимость не спасает от ловушек:
2140 if ((t = HIT_TRAP) or (t = HIT_SELF)) and (not FGodMode) then
2141 begin
2142 if not g_Game_IsClient then
2143 begin
2144 FArmor := 0;
2145 if t = HIT_TRAP then
2146 begin
2147 // Ловушка убивает сразу:
2148 FHealth := -100;
2149 Kill(K_EXTRAHARDKILL, SpawnerUID, t);
2150 end;
2151 if t = HIT_SELF then
2152 begin
2153 // Самоубийство:
2154 FHealth := 0;
2155 Kill(K_SIMPLEKILL, SpawnerUID, t);
2156 end;
2157 end;
2158 // Обнулить действия примочек, чтобы фон пропал
2159 FMegaRulez[MR_SUIT] := 0;
2160 FMegaRulez[MR_INVUL] := 0;
2161 FMegaRulez[MR_INVIS] := 0;
2162 FSpawnInvul := 0;
2163 FBerserk := 0;
2164 end;
2166 // Но от остального спасает:
2167 if FMegaRulez[MR_INVUL] >= gTime then
2168 Exit;
2170 // Чит-код "ГОРЕЦ":
2171 if FGodMode then
2172 Exit;
2174 // Если есть урон своим, или ранил сам себя, или тебя ранил противник:
2175 if LongBool(gGameSettings.Options and GAME_OPTION_TEAMDAMAGE) or
2176 (SpawnerUID = FUID) or
2177 (not SameTeam(FUID, SpawnerUID)) then
2178 begin
2179 FLastSpawnerUID := SpawnerUID;
2181 // Кровь (пузырьки, если в воде):
2182 if gBloodCount > 0 then
2183 begin
2184 c := Min(value, 200)*gBloodCount + Random(Min(value, 200) div 2);
2185 if value div 4 <= c then
2186 c := c - (value div 4)
2187 else
2188 c := 0;
2190 if (t = HIT_SOME) and (vx = 0) and (vy = 0) then
2191 MakeBloodSimple(c)
2192 else
2193 case t of
2194 HIT_TRAP, HIT_ACID, HIT_FLAME, HIT_SELF: MakeBloodSimple(c);
2195 HIT_BFG, HIT_ROCKET, HIT_SOME: MakeBloodVector(c, vx, vy);
2196 end;
2198 if t = HIT_WATER then
2199 g_GFX_Bubbles(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2),
2200 FObj.Y+PLAYER_RECT.Y-4, value div 2, 8, 4);
2201 end;
2203 // Буфер урона:
2204 if FAlive then
2205 Inc(FDamageBuffer, value);
2207 // Вспышка боли:
2208 if gFlash <> 0 then
2209 FPain := FPain + value;
2210 end;
2212 if g_Game_IsServer and g_Game_IsNet then
2213 begin
2214 MH_SEND_PlayerDamage(FUID, t, SpawnerUID, value, vx, vy);
2215 MH_SEND_PlayerStats(FUID);
2216 MH_SEND_PlayerPos(False, FUID);
2217 end;
2218 end;
2220 function TPlayer.Heal(value: Word; Soft: Boolean): Boolean;
2221 begin
2222 Result := False;
2223 if g_Game_IsClient then
2224 Exit;
2225 if not FAlive then
2226 Exit;
2228 if Soft and (FHealth < PLAYER_HP_SOFT) then
2229 begin
2230 IncMax(FHealth, value, PLAYER_HP_SOFT);
2231 Result := True;
2232 end;
2233 if (not Soft) and (FHealth < PLAYER_HP_LIMIT) then
2234 begin
2235 IncMax(FHealth, value, PLAYER_HP_LIMIT);
2236 Result := True;
2237 end;
2239 if Result and g_Game_IsServer and g_Game_IsNet then
2240 MH_SEND_PlayerStats(FUID);
2241 end;
2243 destructor TPlayer.Destroy();
2244 begin
2245 if (gPlayer1 <> nil) and (gPlayer1.FUID = FUID) then
2246 gPlayer1 := nil;
2247 if (gPlayer2 <> nil) and (gPlayer2.FUID = FUID) then
2248 gPlayer2 := nil;
2250 FSawSound.Free();
2251 FSawSoundIdle.Free();
2252 FSawSoundHit.Free();
2253 FSawSoundSelect.Free();
2254 FFlameSoundOn.Free();
2255 FFlameSoundOff.Free();
2256 FFlameSoundWork.Free();
2257 FJetSoundFly.Free();
2258 FJetSoundOn.Free();
2259 FJetSoundOff.Free();
2260 FModel.Free();
2261 FPunchAnim.Free();
2263 inherited;
2264 end;
2266 procedure TPlayer.DoPunch();
2267 begin
2268 FPunchAnim.Reset;
2269 FPunchAnim.Enable;
2270 end;
2272 procedure TPlayer.Fire();
2273 var
2274 f, DidFire: Boolean;
2275 wx, wy, xd, yd: Integer;
2276 locobj: TObj;
2277 begin
2278 if g_Game_IsClient then Exit;
2279 // FBFGFireCounter - время перед выстрелом (для BFG)
2280 // FReloading - время после выстрела (для всего)
2282 if FSpectator then
2283 begin
2284 Respawn(False);
2285 Exit;
2286 end;
2288 if FReloading[FCurrWeap] <> 0 then Exit;
2290 DidFire := False;
2292 f := False;
2293 wx := FObj.X+WEAPONPOINT[FDirection].X;
2294 wy := FObj.Y+WEAPONPOINT[FDirection].Y;
2295 xd := wx+IfThen(FDirection = TDirection.D_LEFT, -30, 30);
2296 yd := wy+firediry();
2298 case FCurrWeap of
2299 WEAPON_KASTET:
2300 begin
2301 DoPunch();
2302 if R_BERSERK in FRulez then
2303 begin
2304 //g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 75, FUID);
2305 locobj.X := FObj.X+FObj.Rect.X;
2306 locobj.Y := FObj.Y+FObj.Rect.Y;
2307 locobj.rect.X := 0;
2308 locobj.rect.Y := 0;
2309 locobj.rect.Width := 39;
2310 locobj.rect.Height := 52;
2311 locobj.Vel.X := (xd-wx) div 2;
2312 locobj.Vel.Y := (yd-wy) div 2;
2313 locobj.Accel.X := xd-wx;
2314 locobj.Accel.y := yd-wy;
2316 if g_Weapon_Hit(@locobj, 50, FUID, HIT_SOME) <> 0 then
2317 g_Sound_PlayExAt('SOUND_WEAPON_HITBERSERK', FObj.X, FObj.Y)
2318 else
2319 g_Sound_PlayExAt('SOUND_WEAPON_MISSBERSERK', FObj.X, FObj.Y);
2321 if (gFlash = 1) and (FPain < 50) then FPain := min(FPain + 25, 50);
2322 end
2323 else
2324 begin
2325 g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 3, FUID);
2326 end;
2328 DidFire := True;
2329 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2330 end;
2332 WEAPON_SAW:
2333 begin
2334 if g_Weapon_chainsaw(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y,
2335 IfThen(gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF], 9, 3), FUID) <> 0 then
2336 begin
2337 FSawSoundSelect.Stop();
2338 FSawSound.Stop();
2339 FSawSoundHit.PlayAt(FObj.X, FObj.Y);
2340 end
2341 else if not FSawSoundHit.IsPlaying() then
2342 begin
2343 FSawSoundSelect.Stop();
2344 FSawSound.PlayAt(FObj.X, FObj.Y);
2345 end;
2347 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2348 DidFire := True;
2349 f := True;
2350 end;
2352 WEAPON_PISTOL:
2353 if FAmmo[A_BULLETS] > 0 then
2354 begin
2355 g_Weapon_pistol(wx, wy, xd, yd, FUID);
2356 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2357 Dec(FAmmo[A_BULLETS]);
2358 FFireAngle := FAngle;
2359 f := True;
2360 DidFire := True;
2361 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
2362 GameVelX, GameVelY-2, SHELL_BULLET);
2363 end;
2365 WEAPON_SHOTGUN1:
2366 if FAmmo[A_SHELLS] > 0 then
2367 begin
2368 g_Weapon_shotgun(wx, wy, xd, yd, FUID);
2369 if not gSoundEffectsDF then g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN', wx, wy);
2370 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2371 Dec(FAmmo[A_SHELLS]);
2372 FFireAngle := FAngle;
2373 f := True;
2374 DidFire := True;
2375 FShellTimer := 10;
2376 FShellType := SHELL_SHELL;
2377 end;
2379 WEAPON_SHOTGUN2:
2380 if FAmmo[A_SHELLS] >= 2 then
2381 begin
2382 g_Weapon_dshotgun(wx, wy, xd, yd, FUID);
2383 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2384 Dec(FAmmo[A_SHELLS], 2);
2385 FFireAngle := FAngle;
2386 f := True;
2387 DidFire := True;
2388 FShellTimer := 13;
2389 FShellType := SHELL_DBLSHELL;
2390 end;
2392 WEAPON_CHAINGUN:
2393 if FAmmo[A_BULLETS] > 0 then
2394 begin
2395 g_Weapon_mgun(wx, wy, xd, yd, FUID);
2396 if not gSoundEffectsDF then g_Sound_PlayExAt('SOUND_WEAPON_FIREPISTOL', wx, wy);
2397 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2398 Dec(FAmmo[A_BULLETS]);
2399 FFireAngle := FAngle;
2400 f := True;
2401 DidFire := True;
2402 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
2403 GameVelX, GameVelY-2, SHELL_BULLET);
2404 end;
2406 WEAPON_ROCKETLAUNCHER:
2407 if FAmmo[A_ROCKETS] > 0 then
2408 begin
2409 g_Weapon_rocket(wx, wy, xd, yd, FUID);
2410 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2411 Dec(FAmmo[A_ROCKETS]);
2412 FFireAngle := FAngle;
2413 f := True;
2414 DidFire := True;
2415 end;
2417 WEAPON_PLASMA:
2418 if FAmmo[A_CELLS] > 0 then
2419 begin
2420 g_Weapon_plasma(wx, wy, xd, yd, FUID);
2421 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2422 Dec(FAmmo[A_CELLS]);
2423 FFireAngle := FAngle;
2424 f := True;
2425 DidFire := True;
2426 end;
2428 WEAPON_BFG:
2429 if (FAmmo[A_CELLS] >= 40) and (FBFGFireCounter = -1) then
2430 begin
2431 FBFGFireCounter := 17;
2432 if not FNoReload then
2433 g_Sound_PlayExAt('SOUND_WEAPON_STARTFIREBFG', FObj.X, FObj.Y);
2434 Dec(FAmmo[A_CELLS], 40);
2435 DidFire := True;
2436 end;
2438 WEAPON_SUPERPULEMET:
2439 if FAmmo[A_SHELLS] > 0 then
2440 begin
2441 g_Weapon_shotgun(wx, wy, xd, yd, FUID);
2442 if not gSoundEffectsDF then g_Sound_PlayExAt('SOUND_WEAPON_FIRECGUN', wx, wy);
2443 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2444 Dec(FAmmo[A_SHELLS]);
2445 FFireAngle := FAngle;
2446 f := True;
2447 DidFire := True;
2448 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
2449 GameVelX, GameVelY-2, SHELL_SHELL);
2450 end;
2452 WEAPON_FLAMETHROWER:
2453 if FAmmo[A_FUEL] > 0 then
2454 begin
2455 g_Weapon_flame(wx, wy, xd, yd, FUID);
2456 FlamerOn;
2457 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2458 Dec(FAmmo[A_FUEL]);
2459 FFireAngle := FAngle;
2460 f := True;
2461 DidFire := True;
2462 end
2463 else
2464 begin
2465 FlamerOff;
2466 if g_Game_IsNet and g_Game_IsServer then MH_SEND_PlayerStats(FUID);
2467 end;
2468 end;
2470 if g_Game_IsNet then
2471 begin
2472 if DidFire then
2473 begin
2474 if FCurrWeap <> WEAPON_BFG then
2475 MH_SEND_PlayerFire(FUID, FCurrWeap, wx, wy, xd, yd, LastShotID)
2476 else
2477 if not FNoReload then
2478 MH_SEND_Sound(FObj.X, FObj.Y, 'SOUND_WEAPON_STARTFIREBFG');
2479 end;
2481 MH_SEND_PlayerStats(FUID);
2482 end;
2484 if not f then Exit;
2486 if (FAngle = 0) or (FAngle = 180) then SetAction(A_ATTACK)
2487 else if (FAngle = ANGLE_LEFTDOWN) or (FAngle = ANGLE_RIGHTDOWN) then SetAction(A_ATTACKDOWN)
2488 else if (FAngle = ANGLE_LEFTUP) or (FAngle = ANGLE_RIGHTUP) then SetAction(A_ATTACKUP);
2489 end;
2491 function TPlayer.GetAmmoByWeapon(Weapon: Byte): Word;
2492 begin
2493 case Weapon of
2494 WEAPON_PISTOL, WEAPON_CHAINGUN: Result := FAmmo[A_BULLETS];
2495 WEAPON_SHOTGUN1, WEAPON_SHOTGUN2, WEAPON_SUPERPULEMET: Result := FAmmo[A_SHELLS];
2496 WEAPON_ROCKETLAUNCHER: Result := FAmmo[A_ROCKETS];
2497 WEAPON_PLASMA, WEAPON_BFG: Result := FAmmo[A_CELLS];
2498 WEAPON_FLAMETHROWER: Result := FAmmo[A_FUEL];
2499 else Result := 0;
2500 end;
2501 end;
2503 function TPlayer.HeadInLiquid(XInc, YInc: Integer): Boolean;
2504 begin
2505 Result := g_Map_CollidePanel(FObj.X+PLAYER_HEADRECT.X+XInc, FObj.Y+PLAYER_HEADRECT.Y+YInc,
2506 PLAYER_HEADRECT.Width, PLAYER_HEADRECT.Height,
2507 PANEL_WATER or PANEL_ACID1 or PANEL_ACID2, True);
2508 end;
2510 procedure TPlayer.FlamerOn;
2511 begin
2512 FFlameSoundOff.Stop();
2513 FFlameSoundOff.SetPosition(0);
2514 if FFlaming then
2515 begin
2516 if (not FFlameSoundOn.IsPlaying()) and (not FFlameSoundWork.IsPlaying()) then
2517 FFlameSoundWork.PlayAt(FObj.X, FObj.Y);
2518 end
2519 else
2520 begin
2521 FFlameSoundOn.PlayAt(FObj.X, FObj.Y);
2522 FFlaming := True;
2523 end;
2524 end;
2526 procedure TPlayer.FlamerOff;
2527 begin
2528 if FFlaming then
2529 begin
2530 FFlameSoundOn.Stop();
2531 FFlameSoundOn.SetPosition(0);
2532 FFlameSoundWork.Stop();
2533 FFlameSoundWork.SetPosition(0);
2534 FFlameSoundOff.PlayAt(FObj.X, FObj.Y);
2535 FFlaming := False;
2536 end;
2537 end;
2539 procedure TPlayer.JetpackOn;
2540 begin
2541 FJetSoundFly.Stop;
2542 FJetSoundOff.Stop;
2543 FJetSoundOn.SetPosition(0);
2544 FJetSoundOn.PlayAt(FObj.X, FObj.Y);
2545 FlySmoke(8);
2546 end;
2548 procedure TPlayer.JetpackOff;
2549 begin
2550 FJetSoundFly.Stop;
2551 FJetSoundOn.Stop;
2552 FJetSoundOff.SetPosition(0);
2553 FJetSoundOff.PlayAt(FObj.X, FObj.Y);
2554 end;
2556 procedure TPlayer.CatchFire(Attacker: Word; Timeout: Integer = PLAYER_BURN_TIME);
2557 begin
2558 if Timeout <= 0 then
2559 exit;
2560 if (FMegaRulez[MR_SUIT] > gTime) or (FMegaRulez[MR_INVUL] > gTime) then
2561 exit; // Не загораемся когда есть защита
2562 if g_Obj_CollidePanel(@FObj, 0, 0, PANEL_WATER or PANEL_ACID1 or PANEL_ACID2) then
2563 exit; // Не подгораем в воде на всякий случай
2564 if FFireTime <= 0 then
2565 g_Sound_PlayExAt('SOUND_IGNITE', FObj.X, FObj.Y);
2566 FFireTime := Timeout;
2567 FFireAttacker := Attacker;
2568 if g_Game_IsNet and g_Game_IsServer then
2569 MH_SEND_PlayerStats(FUID);
2570 end;
2572 procedure TPlayer.Jump();
2573 begin
2574 if gFly or FJetpack then
2575 begin
2576 // Полет (чит-код или джетпак):
2577 if FObj.Vel.Y > -VEL_FLY then
2578 FObj.Vel.Y := FObj.Vel.Y - 3;
2579 if FJetpack then
2580 begin
2581 if FJetFuel > 0 then
2582 Dec(FJetFuel);
2583 if (FJetFuel < 1) and g_Game_IsServer then
2584 begin
2585 FJetpack := False;
2586 JetpackOff;
2587 if g_Game_IsNet then
2588 MH_SEND_PlayerStats(FUID);
2589 end;
2590 end;
2591 Exit;
2592 end;
2594 // Не включать джетпак в режиме прохождения сквозь стены
2595 if FGhost then
2596 FCanJetpack := False;
2598 // Прыгаем или всплываем:
2599 if (CollideLevel(0, 1) or
2600 g_Map_CollidePanel(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y+36, PLAYER_RECT.Width,
2601 PLAYER_RECT.Height-33, PANEL_STEP, False)
2602 ) and (FObj.Accel.Y = 0) then // Не прыгать, если есть вертикальное ускорение
2603 begin
2604 FObj.Vel.Y := -VEL_JUMP;
2605 FCanJetpack := False;
2606 end
2607 else
2608 begin
2609 if BodyInLiquid(0, 0) then
2610 FObj.Vel.Y := -VEL_SW
2611 else if (FJetFuel > 0) and FCanJetpack and
2612 g_Game_IsServer and (not g_Obj_CollideLiquid(@FObj, 0, 0)) then
2613 begin
2614 FJetpack := True;
2615 JetpackOn;
2616 if g_Game_IsNet then
2617 MH_SEND_PlayerStats(FUID);
2618 end;
2619 end;
2620 end;
2622 procedure TPlayer.Kill(KillType: Byte; SpawnerUID: Word; t: Byte);
2623 var
2624 a, i, k, ab, ar: Byte;
2625 s: String;
2626 mon: TMonster;
2627 plr: TPlayer;
2628 srv, netsrv: Boolean;
2629 DoFrags: Boolean;
2630 OldLR: Byte;
2631 KP: TPlayer;
2632 it: PItem;
2634 procedure PushItem(t: Byte);
2635 var
2636 id: DWORD;
2637 begin
2638 id := g_Items_Create(FObj.X, FObj.Y, t, True, False);
2639 it := g_Items_ByIdx(id);
2640 if KillType = K_EXTRAHARDKILL then // -7..+7; -8..0
2641 begin
2642 g_Obj_Push(@it.Obj, (FObj.Vel.X div 2)-7+Random(15),
2643 (FObj.Vel.Y div 2)-Random(9));
2644 it.positionChanged(); // this updates spatial accelerators
2645 end
2646 else
2647 begin
2648 if KillType = K_HARDKILL then // -5..+5; -5..0
2649 begin
2650 g_Obj_Push(@it.Obj, (FObj.Vel.X div 2)-5+Random(11),
2651 (FObj.Vel.Y div 2)-Random(6));
2652 end
2653 else // -3..+3; -3..0
2654 begin
2655 g_Obj_Push(@it.Obj, (FObj.Vel.X div 2)-3+Random(7),
2656 (FObj.Vel.Y div 2)-Random(4));
2657 end;
2658 it.positionChanged(); // this updates spatial accelerators
2659 end;
2661 if g_Game_IsNet and g_Game_IsServer then
2662 MH_SEND_ItemSpawn(True, id);
2663 end;
2665 begin
2666 DoFrags := (gGameSettings.MaxLives = 0) or (gGameSettings.GameMode = GM_COOP);
2667 Srv := g_Game_IsServer;
2668 Netsrv := g_Game_IsServer and g_Game_IsNet;
2669 if Srv then FDeath := FDeath + 1;
2670 if FAlive then
2671 begin
2672 if FGhost then
2673 FGhost := False;
2674 if not FPhysics then
2675 FPhysics := True;
2676 FAlive := False;
2677 end;
2678 FShellTimer := -1;
2680 if (gGameSettings.MaxLives > 0) and Srv and (gLMSRespawn = LMS_RESPAWN_NONE) then
2681 begin
2682 if FLives > 0 then FLives := FLives - 1;
2683 if FLives = 0 then FNoRespawn := True;
2684 end;
2686 // Номер типа смерти:
2687 a := 1;
2688 case KillType of
2689 K_SIMPLEKILL: a := 1;
2690 K_HARDKILL: a := 2;
2691 K_EXTRAHARDKILL: a := 3;
2692 K_FALLKILL: a := 4;
2693 end;
2695 // Звук смерти:
2696 if not FModel.PlaySound(MODELSOUND_DIE, a, FObj.X, FObj.Y) then
2697 for i := 1 to 3 do
2698 if FModel.PlaySound(MODELSOUND_DIE, i, FObj.X, FObj.Y) then
2699 Break;
2701 // Время респауна:
2702 if Srv then
2703 case KillType of
2704 K_SIMPLEKILL:
2705 FTime[T_RESPAWN] := gTime + TIME_RESPAWN1;
2706 K_HARDKILL:
2707 FTime[T_RESPAWN] := gTime + TIME_RESPAWN2;
2708 K_EXTRAHARDKILL, K_FALLKILL:
2709 FTime[T_RESPAWN] := gTime + TIME_RESPAWN3;
2710 end;
2712 // Переключаем состояние:
2713 case KillType of
2714 K_SIMPLEKILL:
2715 SetAction(A_DIE1);
2716 K_HARDKILL, K_EXTRAHARDKILL:
2717 SetAction(A_DIE2);
2718 end;
2720 // Реакция монстров на смерть игрока:
2721 if (KillType <> K_FALLKILL) and (Srv) then
2722 g_Monsters_killedp();
2724 if SpawnerUID = FUID then
2725 begin // Самоубился
2726 if Srv and (DoFrags or (gGameSettings.GameMode = GM_TDM)) then
2727 begin
2728 Dec(FFrags);
2729 FLastFrag := 0;
2730 end;
2731 g_Console_Add(Format(_lc[I_PLAYER_KILL_SELF], [FName]), True);
2732 end
2733 else
2734 if g_GetUIDType(SpawnerUID) = UID_PLAYER then
2735 begin // Убит другим игроком
2736 KP := g_Player_Get(SpawnerUID);
2737 if (KP <> nil) and Srv then
2738 begin
2739 if (DoFrags or (gGameSettings.GameMode = GM_TDM)) then
2740 if SameTeam(FUID, SpawnerUID) then
2741 begin
2742 Dec(KP.FFrags);
2743 KP.FLastFrag := 0;
2744 end else
2745 begin
2746 Inc(KP.FFrags);
2747 KP.FragCombo();
2748 end;
2750 if (gGameSettings.GameMode = GM_TDM) and DoFrags then
2751 Inc(gTeamStat[KP.Team].Goals,
2752 IfThen(SameTeam(FUID, SpawnerUID), -1, 1));
2754 if netsrv then MH_SEND_PlayerStats(SpawnerUID);
2755 end;
2757 plr := g_Player_Get(SpawnerUID);
2758 if plr = nil then
2759 s := '?'
2760 else
2761 s := plr.FName;
2763 case KillType of
2764 K_HARDKILL:
2765 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_2],
2766 [FName, s]),
2767 gShowKillMsg);
2768 K_EXTRAHARDKILL:
2769 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_1],
2770 [FName, s]),
2771 gShowKillMsg);
2772 else
2773 g_Console_Add(Format(_lc[I_PLAYER_KILL],
2774 [FName, s]),
2775 gShowKillMsg);
2776 end;
2777 end
2778 else if g_GetUIDType(SpawnerUID) = UID_MONSTER then
2779 begin // Убит монстром
2780 mon := g_Monsters_ByUID(SpawnerUID);
2781 if mon = nil then
2782 s := '?'
2783 else
2784 s := g_Mons_GetKilledByTypeId(mon.MonsterType);
2786 case KillType of
2787 K_HARDKILL:
2788 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_2],
2789 [FName, s]),
2790 gShowKillMsg);
2791 K_EXTRAHARDKILL:
2792 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_1],
2793 [FName, s]),
2794 gShowKillMsg);
2795 else
2796 g_Console_Add(Format(_lc[I_PLAYER_KILL],
2797 [FName, s]),
2798 gShowKillMsg);
2799 end;
2800 end
2801 else // Особые типы смерти
2802 case t of
2803 HIT_DISCON: ;
2804 HIT_SELF: g_Console_Add(Format(_lc[I_PLAYER_KILL_SELF], [FName]), True);
2805 HIT_FALL: g_Console_Add(Format(_lc[I_PLAYER_KILL_FALL], [FName]), True);
2806 HIT_WATER: g_Console_Add(Format(_lc[I_PLAYER_KILL_WATER], [FName]), True);
2807 HIT_ACID: g_Console_Add(Format(_lc[I_PLAYER_KILL_ACID], [FName]), True);
2808 HIT_TRAP: g_Console_Add(Format(_lc[I_PLAYER_KILL_TRAP], [FName]), True);
2809 else g_Console_Add(Format(_lc[I_PLAYER_DIED], [FName]), True);
2810 end;
2812 if Srv then
2813 begin
2814 // Выброс оружия:
2815 for a := WP_FIRST to WP_LAST do
2816 if FWeapon[a] then
2817 begin
2818 case a of
2819 WEAPON_SAW: i := ITEM_WEAPON_SAW;
2820 WEAPON_SHOTGUN1: i := ITEM_WEAPON_SHOTGUN1;
2821 WEAPON_SHOTGUN2: i := ITEM_WEAPON_SHOTGUN2;
2822 WEAPON_CHAINGUN: i := ITEM_WEAPON_CHAINGUN;
2823 WEAPON_ROCKETLAUNCHER: i := ITEM_WEAPON_ROCKETLAUNCHER;
2824 WEAPON_PLASMA: i := ITEM_WEAPON_PLASMA;
2825 WEAPON_BFG: i := ITEM_WEAPON_BFG;
2826 WEAPON_SUPERPULEMET: i := ITEM_WEAPON_SUPERPULEMET;
2827 WEAPON_FLAMETHROWER: i := ITEM_WEAPON_FLAMETHROWER;
2828 else i := 0;
2829 end;
2831 if i <> 0 then
2832 PushItem(i);
2833 end;
2835 // Выброс рюкзака:
2836 if R_ITEM_BACKPACK in FRulez then
2837 PushItem(ITEM_AMMO_BACKPACK);
2839 // Выброс ракетного ранца:
2840 if FJetFuel > 0 then
2841 PushItem(ITEM_JETPACK);
2843 // Выброс ключей:
2844 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) or
2845 (not LongBool(gGameSettings.Options and GAME_OPTION_DMKEYS)) then
2846 begin
2847 if R_KEY_RED in FRulez then
2848 PushItem(ITEM_KEY_RED);
2850 if R_KEY_GREEN in FRulez then
2851 PushItem(ITEM_KEY_GREEN);
2853 if R_KEY_BLUE in FRulez then
2854 PushItem(ITEM_KEY_BLUE);
2855 end;
2857 // Выброс флага:
2858 DropFlag(KillType = K_FALLKILL);
2859 end;
2861 FCorpse := g_Player_CreateCorpse(Self);
2863 if Srv and (gGameSettings.MaxLives > 0) and FNoRespawn and
2864 (gLMSRespawn = LMS_RESPAWN_NONE) then
2865 begin
2866 a := 0;
2867 k := 0;
2868 ar := 0;
2869 ab := 0;
2870 for i := Low(gPlayers) to High(gPlayers) do
2871 begin
2872 if gPlayers[i] = nil then continue;
2873 if (not gPlayers[i].FNoRespawn) and (not gPlayers[i].FSpectator) then
2874 begin
2875 Inc(a);
2876 if gPlayers[i].FTeam = TEAM_RED then Inc(ar)
2877 else if gPlayers[i].FTeam = TEAM_BLUE then Inc(ab);
2878 k := i;
2879 end;
2880 end;
2882 OldLR := gLMSRespawn;
2883 if (gGameSettings.GameMode = GM_COOP) then
2884 begin
2885 if (a = 0) then
2886 begin
2887 // everyone is dead, restart the map
2888 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
2889 if Netsrv then
2890 MH_SEND_GameEvent(NET_EV_LMS_LOSE);
2891 gLMSRespawn := LMS_RESPAWN_FINAL;
2892 gLMSRespawnTime := gTime + 5000;
2893 end
2894 else if (a = 1) then
2895 begin
2896 if (gPlayers[k] <> nil) and not (gPlayers[k] is TBot) then
2897 if (gPlayers[k] = gPlayer1) or
2898 (gPlayers[k] = gPlayer2) then
2899 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True)
2900 else if Netsrv and (gPlayers[k].FClientID >= 0) then
2901 MH_SEND_GameEvent(NET_EV_LMS_SURVIVOR, 0, 'N', gPlayers[k].FClientID);
2902 end;
2903 end
2904 else if (gGameSettings.GameMode = GM_TDM) then
2905 begin
2906 if (ab = 0) and (ar <> 0) then
2907 begin
2908 // blu team ded
2909 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
2910 if Netsrv then
2911 MH_SEND_GameEvent(NET_EV_TLMS_WIN, TEAM_RED);
2912 Inc(gTeamStat[TEAM_RED].Goals);
2913 gLMSRespawn := LMS_RESPAWN_FINAL;
2914 gLMSRespawnTime := gTime + 5000;
2915 end
2916 else if (ar = 0) and (ab <> 0) then
2917 begin
2918 // red team ded
2919 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
2920 if Netsrv then
2921 MH_SEND_GameEvent(NET_EV_TLMS_WIN, TEAM_BLUE);
2922 Inc(gTeamStat[TEAM_BLUE].Goals);
2923 gLMSRespawn := LMS_RESPAWN_FINAL;
2924 gLMSRespawnTime := gTime + 5000;
2925 end
2926 else if (ar = 0) and (ab = 0) then
2927 begin
2928 // everyone ded
2929 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
2930 if Netsrv then
2931 MH_SEND_GameEvent(NET_EV_LMS_DRAW, 0, FName);
2932 gLMSRespawn := LMS_RESPAWN_FINAL;
2933 gLMSRespawnTime := gTime + 5000;
2934 end;
2935 end
2936 else if (gGameSettings.GameMode = GM_DM) then
2937 begin
2938 if (a = 1) then
2939 begin
2940 if gPlayers[k] <> nil then
2941 with gPlayers[k] do
2942 begin
2943 // survivor is the winner
2944 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(FName)]), 144);
2945 if Netsrv then
2946 MH_SEND_GameEvent(NET_EV_LMS_WIN, 0, FName);
2947 Inc(FFrags);
2948 end;
2949 gLMSRespawn := LMS_RESPAWN_FINAL;
2950 gLMSRespawnTime := gTime + 5000;
2951 end
2952 else if (a = 0) then
2953 begin
2954 // everyone is dead, restart the map
2955 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
2956 if Netsrv then
2957 MH_SEND_GameEvent(NET_EV_LMS_DRAW, 0, FName);
2958 gLMSRespawn := LMS_RESPAWN_FINAL;
2959 gLMSRespawnTime := gTime + 5000;
2960 end;
2961 end;
2962 if srv and (OldLR = LMS_RESPAWN_NONE) and (gLMSRespawn > LMS_RESPAWN_NONE) then
2963 begin
2964 if NetMode = NET_SERVER then
2965 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, gLMSRespawnTime - gTime)
2966 else
2967 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [(gLMSRespawnTime - gTime) div 1000]), True);
2968 end;
2969 end;
2971 if Netsrv then
2972 begin
2973 MH_SEND_PlayerStats(FUID);
2974 MH_SEND_PlayerDeath(FUID, KillType, t, SpawnerUID);
2975 if gGameSettings.GameMode = GM_TDM then MH_SEND_GameStats;
2976 end;
2978 if srv and FNoRespawn then Spectate(True);
2979 FWantsInGame := True;
2980 end;
2982 function TPlayer.BodyInLiquid(XInc, YInc: Integer): Boolean;
2983 begin
2984 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc, PLAYER_RECT.Width,
2985 PLAYER_RECT.Height-20, PANEL_WATER or PANEL_ACID1 or PANEL_ACID2, False);
2986 end;
2988 function TPlayer.BodyInAcid(XInc, YInc: Integer): Boolean;
2989 begin
2990 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc, PLAYER_RECT.Width,
2991 PLAYER_RECT.Height-20, PANEL_ACID1 or PANEL_ACID2, False);
2992 end;
2994 procedure TPlayer.MakeBloodSimple(Count: Word);
2995 var Blood: TModelBlood;
2996 begin
2997 Blood := SELF.FModel.GetBlood();
2998 g_GFX_Blood(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)+8,
2999 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2),
3000 Count div 2, 3, -1, 16, (PLAYER_RECT.Height*2 div 3),
3001 Blood.R, Blood.G, Blood.B, Blood.Kind);
3002 g_GFX_Blood(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-8,
3003 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2),
3004 Count div 2, -3, -1, 16, (PLAYER_RECT.Height*2) div 3,
3005 Blood.R, Blood.G, Blood.B, Blood.Kind);
3006 end;
3008 procedure TPlayer.MakeBloodVector(Count: Word; VelX, VelY: Integer);
3009 var Blood: TModelBlood;
3010 begin
3011 Blood := SELF.FModel.GetBlood();
3012 g_GFX_Blood(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2),
3013 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2),
3014 Count, VelX, VelY, 16, (PLAYER_RECT.Height*2) div 3,
3015 Blood.R, Blood.G, Blood.B, Blood.Kind);
3016 end;
3018 procedure TPlayer.QueueWeaponSwitch(Weapon: Byte);
3019 begin
3020 if g_Game_IsClient then Exit;
3021 if Weapon > High(FWeapon) then Exit;
3022 FNextWeap := FNextWeap or (1 shl Weapon);
3023 end;
3025 procedure TPlayer.resetWeaponQueue ();
3026 begin
3027 FNextWeap := 0;
3028 FNextWeapDelay := 0;
3029 end;
3031 function TPlayer.hasAmmoForWeapon (weapon: Byte): Boolean;
3032 begin
3033 result := false;
3034 case weapon of
3035 WEAPON_KASTET, WEAPON_SAW: result := true;
3036 WEAPON_SHOTGUN1, WEAPON_SHOTGUN2, WEAPON_SUPERPULEMET: result := (FAmmo[A_SHELLS] > 0);
3037 WEAPON_PISTOL, WEAPON_CHAINGUN: result := (FAmmo[A_BULLETS] > 0);
3038 WEAPON_ROCKETLAUNCHER: result := (FAmmo[A_ROCKETS] > 0);
3039 WEAPON_PLASMA, WEAPON_BFG: result := (FAmmo[A_CELLS] > 0);
3040 WEAPON_FLAMETHROWER: result := (FAmmo[A_FUEL] > 0);
3041 else result := (weapon < length(FWeapon));
3042 end;
3043 end;
3045 // return 255 for "no switch"
3046 function TPlayer.getNextWeaponIndex (): Byte;
3047 var
3048 i: Word;
3049 wantThisWeapon: array[0..64] of Boolean;
3050 wwc: Integer = 0; //HACK!
3051 dir, cwi: Integer;
3052 begin
3053 result := 255; // default result: "no switch"
3054 // had weapon cycling on previous frame? remove that flag
3055 if (FNextWeap and $2000) <> 0 then
3056 begin
3057 FNextWeap := FNextWeap and $1FFF;
3058 FNextWeapDelay := 0;
3059 end;
3060 // cycling has priority
3061 if (FNextWeap and $C000) <> 0 then
3062 begin
3063 if (FNextWeap and $8000) <> 0 then
3064 dir := 1
3065 else
3066 dir := -1;
3067 FNextWeap := FNextWeap or $2000; // we need this
3068 if FNextWeapDelay > 0 then
3069 exit; // cooldown time
3070 cwi := FCurrWeap;
3071 for i := 0 to High(FWeapon) do
3072 begin
3073 cwi := (cwi+length(FWeapon)+dir) mod length(FWeapon);
3074 if FWeapon[cwi] then
3075 begin
3076 //e_WriteLog(Format(' SWITCH: cur=%d; new=%d', [FCurrWeap, cwi]), MSG_WARNING);
3077 result := Byte(cwi);
3078 FNextWeapDelay := WEAPON_DELAY;
3079 exit;
3080 end;
3081 end;
3082 resetWeaponQueue();
3083 exit;
3084 end;
3085 // no cycling
3086 for i := 0 to High(wantThisWeapon) do
3087 wantThisWeapon[i] := false;
3088 for i := 0 to High(FWeapon) do
3089 if (FNextWeap and (1 shl i)) <> 0 then
3090 begin
3091 wantThisWeapon[i] := true;
3092 Inc(wwc);
3093 end;
3094 // exclude currently selected weapon from the set
3095 wantThisWeapon[FCurrWeap] := false;
3096 // slow down alterations a little
3097 if wwc > 1 then
3098 begin
3099 //e_WriteLog(Format(' FNextWeap=%x; delay=%d', [FNextWeap, FNextWeapDelay]), MSG_WARNING);
3100 // more than one weapon requested, assume "alteration" and check alteration delay
3101 if FNextWeapDelay > 0 then
3102 begin
3103 FNextWeap := 0;
3104 exit;
3105 end; // yeah
3106 end;
3107 // do not reset weapon queue, it will be done in `RealizeCurrentWeapon()`
3108 // but clear all counters if no weapon should be switched
3109 if wwc < 1 then
3110 begin
3111 resetWeaponQueue();
3112 exit;
3113 end;
3114 //e_WriteLog(Format('wwc=%d', [wwc]), MSG_WARNING);
3115 // try weapons in descending order
3116 for i := High(FWeapon) downto 0 do
3117 begin
3118 if wantThisWeapon[i] and FWeapon[i] and ((wwc = 1) or hasAmmoForWeapon(i)) then
3119 begin
3120 // i found her!
3121 result := Byte(i);
3122 resetWeaponQueue();
3123 FNextWeapDelay := WEAPON_DELAY * 2; // anyway, 'cause why not
3124 exit;
3125 end;
3126 end;
3127 // no suitable weapon found, so reset the queue, to avoid accidental "queuing" of weapon w/o ammo
3128 resetWeaponQueue();
3129 end;
3131 procedure TPlayer.RealizeCurrentWeapon();
3132 function switchAllowed (): Boolean;
3133 var
3134 i: Byte;
3135 begin
3136 result := false;
3137 if FBFGFireCounter <> -1 then
3138 exit;
3139 if FTime[T_SWITCH] > gTime then
3140 exit;
3141 for i := WP_FIRST to WP_LAST do
3142 if FReloading[i] > 0 then
3143 exit;
3144 result := true;
3145 end;
3147 var
3148 nw: Byte;
3149 begin
3150 //e_WriteLog(Format('***RealizeCurrentWeapon: FNextWeap=%x; FNextWeapDelay=%d', [FNextWeap, FNextWeapDelay]), MSG_WARNING);
3151 //FNextWeap := FNextWeap and $1FFF;
3152 if FNextWeapDelay > 0 then Dec(FNextWeapDelay); // "alteration delay"
3154 if not switchAllowed then
3155 begin
3156 //HACK for weapon cycling
3157 if (FNextWeap and $E000) <> 0 then FNextWeap := 0;
3158 exit;
3159 end;
3161 nw := getNextWeaponIndex();
3162 if nw = 255 then exit; // don't reset anything here
3163 if nw > High(FWeapon) then
3164 begin
3165 // don't forget to reset queue here!
3166 //e_WriteLog(' RealizeCurrentWeapon: WUTAFUUUU', MSG_WARNING);
3167 resetWeaponQueue();
3168 exit;
3169 end;
3171 if FWeapon[nw] then
3172 begin
3173 FCurrWeap := nw;
3174 FTime[T_SWITCH] := gTime+156;
3175 if FCurrWeap = WEAPON_SAW then FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
3176 FModel.SetWeapon(FCurrWeap);
3177 if g_Game_IsNet then MH_SEND_PlayerStats(FUID);
3178 end;
3179 end;
3181 procedure TPlayer.NextWeapon();
3182 begin
3183 if g_Game_IsClient then Exit;
3184 FNextWeap := $8000;
3185 end;
3187 procedure TPlayer.PrevWeapon();
3188 begin
3189 if g_Game_IsClient then Exit;
3190 FNextWeap := $4000;
3191 end;
3193 procedure TPlayer.SetWeapon(W: Byte);
3194 begin
3195 if FCurrWeap <> W then
3196 if W = WEAPON_SAW then
3197 FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
3199 FCurrWeap := W;
3200 FModel.SetWeapon(CurrWeap);
3201 resetWeaponQueue();
3202 end;
3204 function TPlayer.PickItem(ItemType: Byte; arespawn: Boolean; var remove: Boolean): Boolean;
3206 function allowBerserkSwitching (): Boolean;
3207 begin
3208 if (FBFGFireCounter <> -1) then begin result := false; exit; end;
3209 result := true;
3210 if gBerserkAutoswitch then exit;
3211 if not conIsCheatsEnabled then exit;
3212 result := false;
3213 end;
3215 var
3216 a: Boolean;
3217 begin
3218 Result := False;
3219 if g_Game_IsClient then Exit;
3221 // a = true - место спавна предмета:
3222 a := LongBool(gGameSettings.Options and GAME_OPTION_WEAPONSTAY) and arespawn;
3223 remove := not a;
3225 case ItemType of
3226 ITEM_MEDKIT_SMALL:
3227 if (FHealth < PLAYER_HP_SOFT) or (FFireTime > 0) then
3228 begin
3229 if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 10, PLAYER_HP_SOFT);
3230 Result := True;
3231 remove := True;
3232 FFireTime := 0;
3233 if gFlash = 2 then Inc(FPickup, 5);
3234 end;
3236 ITEM_MEDKIT_LARGE:
3237 if (FHealth < PLAYER_HP_SOFT) or (FFireTime > 0) then
3238 begin
3239 if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 25, PLAYER_HP_SOFT);
3240 Result := True;
3241 remove := True;
3242 FFireTime := 0;
3243 if gFlash = 2 then Inc(FPickup, 5);
3244 end;
3246 ITEM_ARMOR_GREEN:
3247 if FArmor < PLAYER_AP_SOFT then
3248 begin
3249 FArmor := PLAYER_AP_SOFT;
3250 Result := True;
3251 remove := True;
3252 if gFlash = 2 then Inc(FPickup, 5);
3253 end;
3255 ITEM_ARMOR_BLUE:
3256 if FArmor < PLAYER_AP_LIMIT then
3257 begin
3258 FArmor := PLAYER_AP_LIMIT;
3259 Result := True;
3260 remove := True;
3261 if gFlash = 2 then Inc(FPickup, 5);
3262 end;
3264 ITEM_SPHERE_BLUE:
3265 if (FHealth < PLAYER_HP_LIMIT) or (FFireTime > 0) then
3266 begin
3267 if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 100, PLAYER_HP_LIMIT);
3268 Result := True;
3269 remove := True;
3270 FFireTime := 0;
3271 if gFlash = 2 then Inc(FPickup, 5);
3272 end;
3274 ITEM_SPHERE_WHITE:
3275 if (FHealth < PLAYER_HP_LIMIT) or (FArmor < PLAYER_AP_LIMIT) or (FFireTime > 0) then
3276 begin
3277 if FHealth < PLAYER_HP_LIMIT then
3278 FHealth := PLAYER_HP_LIMIT;
3279 if FArmor < PLAYER_AP_LIMIT then
3280 FArmor := PLAYER_AP_LIMIT;
3281 Result := True;
3282 remove := True;
3283 FFireTime := 0;
3284 if gFlash = 2 then Inc(FPickup, 5);
3285 end;
3287 ITEM_WEAPON_SAW:
3288 if (not FWeapon[WEAPON_SAW]) or ((not arespawn) and (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) then
3289 begin
3290 FWeapon[WEAPON_SAW] := True;
3291 Result := True;
3292 if gFlash = 2 then Inc(FPickup, 5);
3293 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3294 end;
3296 ITEM_WEAPON_SHOTGUN1:
3297 if (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or not FWeapon[WEAPON_SHOTGUN1] then
3298 begin
3299 // Нужно, чтобы не взять все пули сразу:
3300 if a and FWeapon[WEAPON_SHOTGUN1] then Exit;
3302 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3303 FWeapon[WEAPON_SHOTGUN1] := True;
3304 Result := True;
3305 if gFlash = 2 then Inc(FPickup, 5);
3306 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3307 end;
3309 ITEM_WEAPON_SHOTGUN2:
3310 if (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or not FWeapon[WEAPON_SHOTGUN2] then
3311 begin
3312 if a and FWeapon[WEAPON_SHOTGUN2] then Exit;
3314 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3315 FWeapon[WEAPON_SHOTGUN2] := True;
3316 Result := True;
3317 if gFlash = 2 then Inc(FPickup, 5);
3318 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3319 end;
3321 ITEM_WEAPON_CHAINGUN:
3322 if (FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS]) or not FWeapon[WEAPON_CHAINGUN] then
3323 begin
3324 if a and FWeapon[WEAPON_CHAINGUN] then Exit;
3326 IncMax(FAmmo[A_BULLETS], 50, FMaxAmmo[A_BULLETS]);
3327 FWeapon[WEAPON_CHAINGUN] := True;
3328 Result := True;
3329 if gFlash = 2 then Inc(FPickup, 5);
3330 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3331 end;
3333 ITEM_WEAPON_ROCKETLAUNCHER:
3334 if (FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS]) or not FWeapon[WEAPON_ROCKETLAUNCHER] then
3335 begin
3336 if a and FWeapon[WEAPON_ROCKETLAUNCHER] then Exit;
3338 IncMax(FAmmo[A_ROCKETS], 2, FMaxAmmo[A_ROCKETS]);
3339 FWeapon[WEAPON_ROCKETLAUNCHER] := True;
3340 Result := True;
3341 if gFlash = 2 then Inc(FPickup, 5);
3342 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3343 end;
3345 ITEM_WEAPON_PLASMA:
3346 if (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or not FWeapon[WEAPON_PLASMA] then
3347 begin
3348 if a and FWeapon[WEAPON_PLASMA] then Exit;
3350 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3351 FWeapon[WEAPON_PLASMA] := True;
3352 Result := True;
3353 if gFlash = 2 then Inc(FPickup, 5);
3354 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3355 end;
3357 ITEM_WEAPON_BFG:
3358 if (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or not FWeapon[WEAPON_BFG] then
3359 begin
3360 if a and FWeapon[WEAPON_BFG] then Exit;
3362 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3363 FWeapon[WEAPON_BFG] := True;
3364 Result := True;
3365 if gFlash = 2 then Inc(FPickup, 5);
3366 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3367 end;
3369 ITEM_WEAPON_SUPERPULEMET:
3370 if (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or not FWeapon[WEAPON_SUPERPULEMET] then
3371 begin
3372 if a and FWeapon[WEAPON_SUPERPULEMET] then Exit;
3374 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3375 FWeapon[WEAPON_SUPERPULEMET] := True;
3376 Result := True;
3377 if gFlash = 2 then Inc(FPickup, 5);
3378 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3379 end;
3381 ITEM_WEAPON_FLAMETHROWER:
3382 if (FAmmo[A_FUEL] < FMaxAmmo[A_FUEL]) or not FWeapon[WEAPON_FLAMETHROWER] then
3383 begin
3384 if a and FWeapon[WEAPON_FLAMETHROWER] then Exit;
3386 IncMax(FAmmo[A_FUEL], 100, FMaxAmmo[A_FUEL]);
3387 FWeapon[WEAPON_FLAMETHROWER] := True;
3388 Result := True;
3389 if gFlash = 2 then Inc(FPickup, 5);
3390 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3391 end;
3393 ITEM_AMMO_BULLETS:
3394 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then
3395 begin
3396 IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
3397 Result := True;
3398 remove := True;
3399 if gFlash = 2 then Inc(FPickup, 5);
3400 end;
3402 ITEM_AMMO_BULLETS_BOX:
3403 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then
3404 begin
3405 IncMax(FAmmo[A_BULLETS], 50, FMaxAmmo[A_BULLETS]);
3406 Result := True;
3407 remove := True;
3408 if gFlash = 2 then Inc(FPickup, 5);
3409 end;
3411 ITEM_AMMO_SHELLS:
3412 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then
3413 begin
3414 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3415 Result := True;
3416 remove := True;
3417 if gFlash = 2 then Inc(FPickup, 5);
3418 end;
3420 ITEM_AMMO_SHELLS_BOX:
3421 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then
3422 begin
3423 IncMax(FAmmo[A_SHELLS], 25, FMaxAmmo[A_SHELLS]);
3424 Result := True;
3425 remove := True;
3426 if gFlash = 2 then Inc(FPickup, 5);
3427 end;
3429 ITEM_AMMO_ROCKET:
3430 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then
3431 begin
3432 IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
3433 Result := True;
3434 remove := True;
3435 if gFlash = 2 then Inc(FPickup, 5);
3436 end;
3438 ITEM_AMMO_ROCKET_BOX:
3439 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then
3440 begin
3441 IncMax(FAmmo[A_ROCKETS], 5, FMaxAmmo[A_ROCKETS]);
3442 Result := True;
3443 remove := True;
3444 if gFlash = 2 then Inc(FPickup, 5);
3445 end;
3447 ITEM_AMMO_CELL:
3448 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then
3449 begin
3450 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3451 Result := True;
3452 remove := True;
3453 if gFlash = 2 then Inc(FPickup, 5);
3454 end;
3456 ITEM_AMMO_CELL_BIG:
3457 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then
3458 begin
3459 IncMax(FAmmo[A_CELLS], 100, FMaxAmmo[A_CELLS]);
3460 Result := True;
3461 remove := True;
3462 if gFlash = 2 then Inc(FPickup, 5);
3463 end;
3465 ITEM_AMMO_FUELCAN:
3466 if FAmmo[A_FUEL] < FMaxAmmo[A_FUEL] then
3467 begin
3468 IncMax(FAmmo[A_FUEL], 100, FMaxAmmo[A_FUEL]);
3469 Result := True;
3470 remove := True;
3471 if gFlash = 2 then Inc(FPickup, 5);
3472 end;
3474 ITEM_AMMO_BACKPACK:
3475 if not(R_ITEM_BACKPACK in FRulez) or
3476 (FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS]) or
3477 (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or
3478 (FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS]) or
3479 (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or
3480 (FAmmo[A_FUEL] < FMaxAmmo[A_FUEL]) then
3481 begin
3482 FMaxAmmo[A_BULLETS] := AmmoLimits[1, A_BULLETS];
3483 FMaxAmmo[A_SHELLS] := AmmoLimits[1, A_SHELLS];
3484 FMaxAmmo[A_ROCKETS] := AmmoLimits[1, A_ROCKETS];
3485 FMaxAmmo[A_CELLS] := AmmoLimits[1, A_CELLS];
3486 FMaxAmmo[A_FUEL] := AmmoLimits[1, A_FUEL];
3488 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then
3489 IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
3490 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then
3491 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3492 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then
3493 IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
3494 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then
3495 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3496 if FAmmo[A_FUEL] < FMaxAmmo[A_FUEL] then
3497 IncMax(FAmmo[A_FUEL], 50, FMaxAmmo[A_FUEL]);
3499 FRulez := FRulez + [R_ITEM_BACKPACK];
3500 Result := True;
3501 remove := True;
3502 if gFlash = 2 then Inc(FPickup, 5);
3503 end;
3505 ITEM_KEY_RED:
3506 if not(R_KEY_RED in FRulez) then
3507 begin
3508 Include(FRulez, R_KEY_RED);
3509 Result := True;
3510 remove := (gGameSettings.GameMode <> GM_COOP) and (g_Player_GetCount() < 2);
3511 if gFlash = 2 then Inc(FPickup, 5);
3512 if (not remove) and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETITEM');
3513 end;
3515 ITEM_KEY_GREEN:
3516 if not(R_KEY_GREEN in FRulez) then
3517 begin
3518 Include(FRulez, R_KEY_GREEN);
3519 Result := True;
3520 remove := (gGameSettings.GameMode <> GM_COOP) and (g_Player_GetCount() < 2);
3521 if gFlash = 2 then Inc(FPickup, 5);
3522 if (not remove) and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETITEM');
3523 end;
3525 ITEM_KEY_BLUE:
3526 if not(R_KEY_BLUE in FRulez) then
3527 begin
3528 Include(FRulez, R_KEY_BLUE);
3529 Result := True;
3530 remove := (gGameSettings.GameMode <> GM_COOP) and (g_Player_GetCount() < 2);
3531 if gFlash = 2 then Inc(FPickup, 5);
3532 if (not remove) and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETITEM');
3533 end;
3535 ITEM_SUIT:
3536 if FMegaRulez[MR_SUIT] < gTime+PLAYER_SUIT_TIME then
3537 begin
3538 FMegaRulez[MR_SUIT] := gTime+PLAYER_SUIT_TIME;
3539 Result := True;
3540 remove := True;
3541 FFireTime := 0;
3542 if gFlash = 2 then Inc(FPickup, 5);
3543 end;
3545 ITEM_OXYGEN:
3546 if FAir < AIR_MAX then
3547 begin
3548 FAir := AIR_MAX;
3549 Result := True;
3550 remove := True;
3551 if gFlash = 2 then Inc(FPickup, 5);
3552 end;
3554 ITEM_MEDKIT_BLACK:
3555 begin
3556 if not (R_BERSERK in FRulez) then
3557 begin
3558 Include(FRulez, R_BERSERK);
3559 if allowBerserkSwitching then
3560 begin
3561 FCurrWeap := WEAPON_KASTET;
3562 resetWeaponQueue();
3563 FModel.SetWeapon(WEAPON_KASTET);
3564 end;
3565 if gFlash <> 0 then
3566 begin
3567 Inc(FPain, 100);
3568 if gFlash = 2 then Inc(FPickup, 5);
3569 end;
3570 FBerserk := gTime+30000;
3571 Result := True;
3572 remove := True;
3573 FFireTime := 0;
3574 end;
3575 if (FHealth < PLAYER_HP_SOFT) or (FFireTime > 0) then
3576 begin
3577 if FHealth < PLAYER_HP_SOFT then FHealth := PLAYER_HP_SOFT;
3578 FBerserk := gTime+30000;
3579 Result := True;
3580 remove := True;
3581 FFireTime := 0;
3582 end;
3583 end;
3585 ITEM_INVUL:
3586 if FMegaRulez[MR_INVUL] < gTime+PLAYER_INVUL_TIME then
3587 begin
3588 FMegaRulez[MR_INVUL] := gTime+PLAYER_INVUL_TIME;
3589 FSpawnInvul := 0;
3590 Result := True;
3591 remove := True;
3592 if gFlash = 2 then Inc(FPickup, 5);
3593 end;
3595 ITEM_BOTTLE:
3596 if (FHealth < PLAYER_HP_LIMIT) or (FFireTime > 0) then
3597 begin
3598 if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 4, PLAYER_HP_LIMIT);
3599 Result := True;
3600 remove := True;
3601 FFireTime := 0;
3602 if gFlash = 2 then Inc(FPickup, 5);
3603 end;
3605 ITEM_HELMET:
3606 if FArmor < PLAYER_AP_LIMIT then
3607 begin
3608 IncMax(FArmor, 5, PLAYER_AP_LIMIT);
3609 Result := True;
3610 remove := True;
3611 if gFlash = 2 then Inc(FPickup, 5);
3612 end;
3614 ITEM_JETPACK:
3615 if FJetFuel < JET_MAX then
3616 begin
3617 FJetFuel := JET_MAX;
3618 Result := True;
3619 remove := True;
3620 if gFlash = 2 then Inc(FPickup, 5);
3621 end;
3623 ITEM_INVIS:
3624 if FMegaRulez[MR_INVIS] < gTime+PLAYER_INVIS_TIME then
3625 begin
3626 FMegaRulez[MR_INVIS] := gTime+PLAYER_INVIS_TIME;
3627 Result := True;
3628 remove := True;
3629 if gFlash = 2 then Inc(FPickup, 5);
3630 end;
3631 end;
3632 end;
3634 procedure TPlayer.Touch();
3635 begin
3636 if not FAlive then
3637 Exit;
3638 //FModel.PlaySound(MODELSOUND_PAIN, 1, FObj.X, FObj.Y);
3639 if FIamBot then
3640 begin
3641 // Бросить флаг товарищу:
3642 if gGameSettings.GameMode = GM_CTF then
3643 DropFlag();
3644 end;
3645 end;
3647 procedure TPlayer.Push(vx, vy: Integer);
3648 begin
3649 if (not FPhysics) and FGhost then
3650 Exit;
3651 FObj.Accel.X := FObj.Accel.X + vx;
3652 FObj.Accel.Y := FObj.Accel.Y + vy;
3653 if g_Game_IsNet and g_Game_IsServer then
3654 MH_SEND_PlayerPos(True, FUID, NET_EVERYONE);
3655 end;
3657 procedure TPlayer.Reset(Force: Boolean);
3658 begin
3659 if Force then
3660 FAlive := False;
3662 FSpawned := False;
3663 FTime[T_RESPAWN] := 0;
3664 FTime[T_FLAGCAP] := 0;
3665 FGodMode := False;
3666 FNoTarget := False;
3667 FNoReload := False;
3668 FFrags := 0;
3669 FLastFrag := 0;
3670 FComboEvnt := -1;
3671 FKills := 0;
3672 FMonsterKills := 0;
3673 FDeath := 0;
3674 FSecrets := 0;
3675 FSpawnInvul := 0;
3676 FCorpse := -1;
3677 FReady := False;
3678 if FNoRespawn then
3679 begin
3680 FSpectator := False;
3681 FGhost := False;
3682 FPhysics := True;
3683 FSpectatePlayer := -1;
3684 FNoRespawn := False;
3685 end;
3686 FLives := gGameSettings.MaxLives;
3688 SetFlag(FLAG_NONE);
3689 end;
3691 procedure TPlayer.SoftReset();
3692 begin
3693 ReleaseKeys();
3695 FDamageBuffer := 0;
3696 FSlopeOld := 0;
3697 FIncCamOld := 0;
3698 FIncCam := 0;
3699 FBFGFireCounter := -1;
3700 FShellTimer := -1;
3701 FPain := 0;
3702 FLastHit := 0;
3703 FLastFrag := 0;
3704 FComboEvnt := -1;
3706 SetFlag(FLAG_NONE);
3707 SetAction(A_STAND, True);
3708 end;
3710 function TPlayer.GetRespawnPoint(): Byte;
3711 var
3712 c: Byte;
3713 begin
3714 Result := 255;
3715 // На будущее: FSpawn - игрок уже играл и перерождается
3717 // Одиночная игра/кооператив
3718 if gGameSettings.GameMode in [GM_COOP, GM_SINGLE] then
3719 begin
3720 if Self = gPlayer1 then
3721 begin
3722 // player 1 should try to spawn on the player 1 point
3723 if g_Map_GetPointCount(RESPAWNPOINT_PLAYER1) > 0 then
3724 Exit(RESPAWNPOINT_PLAYER1)
3725 else if g_Map_GetPointCount(RESPAWNPOINT_PLAYER2) > 0 then
3726 Exit(RESPAWNPOINT_PLAYER2);
3727 end
3728 else if Self = gPlayer2 then
3729 begin
3730 // player 2 should try to spawn on the player 2 point
3731 if g_Map_GetPointCount(RESPAWNPOINT_PLAYER2) > 0 then
3732 Exit(RESPAWNPOINT_PLAYER2)
3733 else if g_Map_GetPointCount(RESPAWNPOINT_PLAYER1) > 0 then
3734 Exit(RESPAWNPOINT_PLAYER1);
3735 end
3736 else
3737 begin
3738 // other players randomly pick either the first or the second point
3739 c := IfThen((Random(2) = 0), RESPAWNPOINT_PLAYER1, RESPAWNPOINT_PLAYER2);
3740 if g_Map_GetPointCount(c) > 0 then
3741 Exit(c);
3742 // try the other one
3743 c := IfThen((c = RESPAWNPOINT_PLAYER1), RESPAWNPOINT_PLAYER2, RESPAWNPOINT_PLAYER1);
3744 if g_Map_GetPointCount(c) > 0 then
3745 Exit(c);
3746 end;
3747 end;
3749 // Мясоповал
3750 if gGameSettings.GameMode = GM_DM then
3751 begin
3752 // try DM points first
3753 if g_Map_GetPointCount(RESPAWNPOINT_DM) > 0 then
3754 Exit(RESPAWNPOINT_DM);
3755 end;
3757 // Командные
3758 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3759 begin
3760 // try team points first
3761 c := RESPAWNPOINT_DM;
3762 if FTeam = TEAM_RED then
3763 c := RESPAWNPOINT_RED
3764 else if FTeam = TEAM_BLUE then
3765 c := RESPAWNPOINT_BLUE;
3766 if g_Map_GetPointCount(c) > 0 then
3767 Exit(c);
3768 end;
3770 // still haven't found a spawnpoint, try random shit
3771 Result := g_Map_GetRandomPointType();
3772 end;
3774 procedure TPlayer.Respawn(Silent: Boolean; Force: Boolean = False);
3775 var
3776 RespawnPoint: TRespawnPoint;
3777 a, b, c: Byte;
3778 begin
3779 FSlopeOld := 0;
3780 FIncCamOld := 0;
3781 FIncCam := 0;
3782 FBFGFireCounter := -1;
3783 FShellTimer := -1;
3784 FPain := 0;
3785 FLastHit := 0;
3786 FSpawnInvul := 0;
3787 FCorpse := -1;
3789 if not g_Game_IsServer then
3790 Exit;
3791 if FDummy then
3792 Exit;
3793 FWantsInGame := True;
3794 FJustTeleported := True;
3795 if Force then
3796 begin
3797 FTime[T_RESPAWN] := 0;
3798 FAlive := False;
3799 end;
3800 FNetTime := 0;
3801 // if server changes MaxLives we gotta be ready
3802 if gGameSettings.MaxLives = 0 then FNoRespawn := False;
3804 // Еще нельзя возродиться:
3805 if FTime[T_RESPAWN] > gTime then
3806 Exit;
3808 // Просрал все жизни:
3809 if FNoRespawn then
3810 begin
3811 if not FSpectator then Spectate(True);
3812 FWantsInGame := True;
3813 Exit;
3814 end;
3816 if (gGameSettings.GameType <> GT_SINGLE) and (gGameSettings.GameMode <> GM_COOP) then
3817 begin // "Своя игра"
3818 // Берсерк не сохраняется между уровнями:
3819 FRulez := FRulez-[R_BERSERK];
3820 end
3821 else // "Одиночная игра"/"Кооп"
3822 begin
3823 // Берсерк и ключи не сохраняются между уровнями:
3824 FRulez := FRulez-[R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE, R_BERSERK];
3825 end;
3827 // Получаем точку спауна игрока:
3828 c := GetRespawnPoint();
3830 ReleaseKeys();
3831 SetFlag(FLAG_NONE);
3833 // Воскрешение без оружия:
3834 if not FAlive then
3835 begin
3836 FHealth := Round(PLAYER_HP_SOFT * (FHandicap / 100));
3837 FArmor := 0;
3838 FAlive := True;
3839 FAir := AIR_DEF;
3840 FJetFuel := 0;
3842 for a := WP_FIRST to WP_LAST do
3843 begin
3844 FWeapon[a] := False;
3845 FReloading[a] := 0;
3846 end;
3848 FWeapon[WEAPON_PISTOL] := True;
3849 FWeapon[WEAPON_KASTET] := True;
3850 FCurrWeap := WEAPON_PISTOL;
3851 resetWeaponQueue();
3853 FModel.SetWeapon(FCurrWeap);
3855 for b := A_BULLETS to A_HIGH do
3856 FAmmo[b] := 0;
3858 FAmmo[A_BULLETS] := 50;
3860 FMaxAmmo[A_BULLETS] := AmmoLimits[0, A_BULLETS];
3861 FMaxAmmo[A_SHELLS] := AmmoLimits[0, A_SHELLS];
3862 FMaxAmmo[A_ROCKETS] := AmmoLimits[0, A_SHELLS];
3863 FMaxAmmo[A_CELLS] := AmmoLimits[0, A_CELLS];
3864 FMaxAmmo[A_FUEL] := AmmoLimits[0, A_FUEL];
3866 if (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF]) and
3867 LongBool(gGameSettings.Options and GAME_OPTION_DMKEYS) then
3868 FRulez := [R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE]
3869 else
3870 FRulez := [];
3871 end;
3873 // Получаем координаты точки возрождения:
3874 if not g_Map_GetPoint(c, RespawnPoint) then
3875 begin
3876 g_FatalError(_lc[I_GAME_ERROR_GET_SPAWN]);
3877 Exit;
3878 end;
3880 // Установка координат и сброс всех параметров:
3881 FObj.X := RespawnPoint.X-PLAYER_RECT.X;
3882 FObj.Y := RespawnPoint.Y-PLAYER_RECT.Y;
3883 FObj.oldX := FObj.X; // don't interpolate after respawn
3884 FObj.oldY := FObj.Y;
3885 FObj.Vel.X := 0;
3886 FObj.Vel.Y := 0;
3887 FObj.Accel.X := 0;
3888 FObj.Accel.Y := 0;
3890 FDirection := RespawnPoint.Direction;
3891 if FDirection = TDirection.D_LEFT then
3892 FAngle := 180
3893 else
3894 FAngle := 0;
3896 SetAction(A_STAND, True);
3897 FModel.Direction := FDirection;
3899 for a := Low(FTime) to High(FTime) do
3900 FTime[a] := 0;
3902 for a := Low(FMegaRulez) to High(FMegaRulez) do
3903 FMegaRulez[a] := 0;
3905 // Respawn invulnerability
3906 if (gGameSettings.GameType <> GT_SINGLE) and (gGameSettings.SpawnInvul > 0) then
3907 begin
3908 FMegaRulez[MR_INVUL] := gTime + gGameSettings.SpawnInvul * 1000;
3909 FSpawnInvul := FMegaRulez[MR_INVUL];
3910 end;
3912 FDamageBuffer := 0;
3913 FJetpack := False;
3914 FCanJetpack := False;
3915 FFlaming := False;
3916 FFireTime := 0;
3917 FFirePainTime := 0;
3918 FFireAttacker := 0;
3920 // Анимация возрождения:
3921 if (not gLoadGameMode) and (not Silent) then
3922 g_GFX_QueueEffect(
3923 R_GFX_TELEPORT_FAST,
3924 FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
3925 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32
3926 );
3928 FSpectator := False;
3929 FGhost := False;
3930 FPhysics := True;
3931 FSpectatePlayer := -1;
3932 FSpawned := True;
3934 if (gPlayer1 = nil) and (gSpectLatchPID1 = FUID) then
3935 gPlayer1 := self;
3936 if (gPlayer2 = nil) and (gSpectLatchPID2 = FUID) then
3937 gPlayer2 := self;
3939 if g_Game_IsNet then
3940 begin
3941 MH_SEND_PlayerPos(True, FUID, NET_EVERYONE);
3942 MH_SEND_PlayerStats(FUID, NET_EVERYONE);
3943 if not Silent then
3944 MH_SEND_Effect(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
3945 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32,
3946 0, NET_GFX_TELE);
3947 end;
3948 end;
3950 procedure TPlayer.Spectate(NoMove: Boolean = False);
3951 begin
3952 if FAlive then
3953 Kill(K_EXTRAHARDKILL, FUID, HIT_SOME)
3954 else if (not NoMove) then
3955 begin
3956 GameX := gMapInfo.Width div 2;
3957 GameY := gMapInfo.Height div 2;
3958 end;
3959 FXTo := GameX;
3960 FYTo := GameY;
3962 FAlive := False;
3963 FSpectator := True;
3964 FGhost := True;
3965 FPhysics := False;
3966 FWantsInGame := False;
3967 FSpawned := False;
3968 FCorpse := -1;
3970 if FNoRespawn then
3971 begin
3972 if Self = gPlayer1 then
3973 begin
3974 gSpectLatchPID1 := FUID;
3975 gPlayer1 := nil;
3976 end
3977 else if Self = gPlayer2 then
3978 begin
3979 gSpectLatchPID2 := FUID;
3980 gPlayer2 := nil;
3981 end;
3982 end;
3984 if g_Game_IsNet then
3985 MH_SEND_PlayerStats(FUID);
3986 end;
3988 procedure TPlayer.SwitchNoClip;
3989 begin
3990 if not FAlive then
3991 Exit;
3992 FGhost := not FGhost;
3993 FPhysics := not FGhost;
3994 if FGhost then
3995 begin
3996 FXTo := FObj.X;
3997 FYTo := FObj.Y;
3998 end else
3999 begin
4000 FObj.Accel.X := 0;
4001 FObj.Accel.Y := 0;
4002 end;
4003 end;
4005 procedure TPlayer.Run(Direction: TDirection);
4006 var
4007 a, b: Integer;
4008 begin
4009 if MAX_RUNVEL > 8 then
4010 FlySmoke();
4012 // Бежим:
4013 if Direction = TDirection.D_LEFT then
4014 begin
4015 if FObj.Vel.X > -MAX_RUNVEL then
4016 FObj.Vel.X := FObj.Vel.X - (MAX_RUNVEL shr 3);
4017 end
4018 else
4019 if FObj.Vel.X < MAX_RUNVEL then
4020 FObj.Vel.X := FObj.Vel.X + (MAX_RUNVEL shr 3);
4022 // Возможно, пинаем куски:
4023 if (FObj.Vel.X <> 0) and (gGibs <> nil) then
4024 begin
4025 b := Abs(FObj.Vel.X);
4026 if b > 1 then b := b * (Random(8 div b) + 1);
4027 for a := 0 to High(gGibs) do
4028 begin
4029 if gGibs[a].alive and
4030 g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y+FObj.Rect.Height-4,
4031 FObj.Rect.Width, 8, @gGibs[a].Obj) and (Random(3) = 0) then
4032 begin
4033 // Пинаем куски
4034 if FObj.Vel.X < 0 then
4035 begin
4036 g_Obj_PushA(@gGibs[a].Obj, b, Random(61)+120) // налево
4037 end
4038 else
4039 begin
4040 g_Obj_PushA(@gGibs[a].Obj, b, Random(61)); // направо
4041 end;
4042 gGibs[a].positionChanged(); // this updates spatial accelerators
4043 end;
4044 end;
4045 end;
4047 SetAction(A_WALK);
4048 end;
4050 procedure TPlayer.SeeDown();
4051 begin
4052 SetAction(A_SEEDOWN);
4054 if FDirection = TDirection.D_LEFT then FAngle := ANGLE_LEFTDOWN else FAngle := ANGLE_RIGHTDOWN;
4056 if FIncCam > -120 then DecMin(FIncCam, 5, -120);
4057 end;
4059 procedure TPlayer.SeeUp();
4060 begin
4061 SetAction(A_SEEUP);
4063 if FDirection = TDirection.D_LEFT then FAngle := ANGLE_LEFTUP else FAngle := ANGLE_RIGHTUP;
4065 if FIncCam < 120 then IncMax(FIncCam, 5, 120);
4066 end;
4068 procedure TPlayer.SetAction(Action: Byte; Force: Boolean = False);
4069 var
4070 Prior: Byte;
4071 begin
4072 case Action of
4073 A_WALK: Prior := 3;
4074 A_DIE1: Prior := 5;
4075 A_DIE2: Prior := 5;
4076 A_ATTACK: Prior := 2;
4077 A_SEEUP: Prior := 1;
4078 A_SEEDOWN: Prior := 1;
4079 A_ATTACKUP: Prior := 2;
4080 A_ATTACKDOWN: Prior := 2;
4081 A_PAIN: Prior := 4;
4082 else Prior := 0;
4083 end;
4085 if (Prior > FActionPrior) or Force then
4086 if not ((Prior = 2) and (FCurrWeap = WEAPON_SAW)) then
4087 begin
4088 FActionPrior := Prior;
4089 FActionAnim := Action;
4090 FActionForce := Force;
4091 FActionChanged := True;
4092 end;
4094 if Action in [A_ATTACK, A_ATTACKUP, A_ATTACKDOWN] then FModel.SetFire(True);
4095 end;
4097 function TPlayer.StayOnStep(XInc, YInc: Integer): Boolean;
4098 begin
4099 Result := not g_Map_CollidePanel(FObj.X+PLAYER_RECT.X, FObj.Y+YInc+PLAYER_RECT.Y+PLAYER_RECT.Height-1,
4100 PLAYER_RECT.Width, 1, PANEL_STEP, False)
4101 and g_Map_CollidePanel(FObj.X+PLAYER_RECT.X, FObj.Y+YInc+PLAYER_RECT.Y+PLAYER_RECT.Height,
4102 PLAYER_RECT.Width, 1, PANEL_STEP, False);
4103 end;
4105 function TPlayer.TeleportTo(X, Y: Integer; silent: Boolean; dir: Byte): Boolean;
4106 begin
4107 Result := False;
4109 if g_CollideLevel(X, Y, PLAYER_RECT.Width, PLAYER_RECT.Height) then
4110 begin
4111 g_Sound_PlayExAt('SOUND_GAME_NOTELEPORT', FObj.X, FObj.Y);
4112 if g_Game_IsServer and g_Game_IsNet then
4113 MH_SEND_Sound(FObj.X, FObj.Y, 'SOUND_GAME_NOTELEPORT');
4114 Exit;
4115 end;
4117 FJustTeleported := True;
4119 if not silent then
4120 begin
4121 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', FObj.X, FObj.Y);
4122 g_GFX_QueueEffect(
4123 R_GFX_TELEPORT_FAST,
4124 FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
4125 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32
4126 );
4127 if g_Game_IsServer and g_Game_IsNet then
4128 MH_SEND_Effect(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
4129 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32, 1,
4130 NET_GFX_TELE);
4131 end;
4133 FObj.X := X-PLAYER_RECT.X;
4134 FObj.Y := Y-PLAYER_RECT.Y;
4135 FObj.oldX := FObj.X; // don't interpolate after respawn
4136 FObj.oldY := FObj.Y;
4137 if FAlive and FGhost then
4138 begin
4139 FXTo := FObj.X;
4140 FYTo := FObj.Y;
4141 end;
4143 if not g_Game_IsNet then
4144 begin
4145 if dir = 1 then
4146 begin
4147 SetDirection(TDirection.D_LEFT);
4148 FAngle := 180;
4149 end
4150 else
4151 if dir = 2 then
4152 begin
4153 SetDirection(TDirection.D_RIGHT);
4154 FAngle := 0;
4155 end
4156 else
4157 if dir = 3 then
4158 begin // обратное
4159 if FDirection = TDirection.D_RIGHT then
4160 begin
4161 SetDirection(TDirection.D_LEFT);
4162 FAngle := 180;
4163 end
4164 else
4165 begin
4166 SetDirection(TDirection.D_RIGHT);
4167 FAngle := 0;
4168 end;
4169 end;
4170 end;
4172 if not silent then
4173 begin
4174 g_GFX_QueueEffect(
4175 R_GFX_TELEPORT_FAST,
4176 FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
4177 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32
4178 );
4179 if g_Game_IsServer and g_Game_IsNet then
4180 MH_SEND_Effect(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
4181 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32, 0,
4182 NET_GFX_TELE);
4183 end;
4185 Result := True;
4186 end;
4188 function nonz(a: Single): Single;
4189 begin
4190 if a <> 0 then
4191 Result := a
4192 else
4193 Result := 1;
4194 end;
4196 function TPlayer.refreshCorpse(): Boolean;
4197 var
4198 i: Integer;
4199 begin
4200 Result := False;
4201 FCorpse := -1;
4202 if FAlive or FSpectator then
4203 Exit;
4204 if (gCorpses = nil) or (Length(gCorpses) = 0) then
4205 Exit;
4206 for i := 0 to High(gCorpses) do
4207 if gCorpses[i] <> nil then
4208 if gCorpses[i].FPlayerUID = FUID then
4209 begin
4210 Result := True;
4211 FCorpse := i;
4212 break;
4213 end;
4214 end;
4216 function TPlayer.getCameraObj(): TObj;
4217 begin
4218 if (not FAlive) and (not FSpectator) and
4219 (FCorpse >= 0) and (FCorpse < Length(gCorpses)) and
4220 (gCorpses[FCorpse] <> nil) and (gCorpses[FCorpse].FPlayerUID = FUID) then
4221 begin
4222 gCorpses[FCorpse].FObj.slopeUpLeft := FObj.slopeUpLeft;
4223 Result := gCorpses[FCorpse].FObj;
4224 end
4225 else
4226 begin
4227 Result := FObj;
4228 end;
4229 end;
4231 procedure TPlayer.PreUpdate();
4232 begin
4233 FSlopeOld := FObj.slopeUpLeft;
4234 FIncCamOld := FIncCam;
4235 FObj.oldX := FObj.X;
4236 FObj.oldY := FObj.Y;
4237 end;
4239 procedure TPlayer.Update();
4240 var
4241 b: Byte;
4242 i, ii, wx, wy, xd, yd, k: Integer;
4243 blockmon, headwater, dospawn: Boolean;
4244 NetServer: Boolean;
4245 AnyServer: Boolean;
4246 SetSpect: Boolean;
4247 begin
4248 NetServer := g_Game_IsNet and g_Game_IsServer;
4249 AnyServer := g_Game_IsServer;
4251 if g_Game_IsClient and (NetInterpLevel > 0) then
4252 DoLerp(NetInterpLevel + 1)
4253 else
4254 if FGhost then
4255 DoLerp(4);
4257 if NetServer then
4258 if FClientID >= 0 then
4259 begin
4260 FPing := NetClients[FClientID].Peer^.lastRoundTripTime;
4261 if NetClients[FClientID].Peer^.packetsSent > 0 then
4262 FLoss := Round(100*NetClients[FClientID].Peer^.packetsLost/NetClients[FClientID].Peer^.packetsSent)
4263 else
4264 FLoss := 0;
4265 end else
4266 begin
4267 FPing := 0;
4268 FLoss := 0;
4269 end;
4271 if FAlive then
4272 FPunchAnim.Update;
4273 if FPunchAnim.played then
4274 FPunchAnim.Disable;
4276 if FAlive and (gFly or FJetpack) then
4277 FlySmoke();
4279 if FDirection = TDirection.D_LEFT then
4280 FAngle := 180
4281 else
4282 FAngle := 0;
4284 if FAlive and (not FGhost) then
4285 begin
4286 if FKeys[KEY_UP].Pressed then
4287 SeeUp();
4288 if FKeys[KEY_DOWN].Pressed then
4289 SeeDown();
4290 end;
4292 if (not (FKeys[KEY_UP].Pressed or FKeys[KEY_DOWN].Pressed)) and
4293 (FIncCam <> 0) then
4294 begin
4295 i := g_basic.Sign(FIncCam);
4296 FIncCam := Abs(FIncCam);
4297 DecMin(FIncCam, 5, 0);
4298 FIncCam := FIncCam*i;
4299 end;
4301 // no need to do that each second frame, weapon queue will take care of it
4302 if FAlive and FKeys[KEY_NEXTWEAPON].Pressed and AnyServer then NextWeapon();
4303 if FAlive and FKeys[KEY_PREVWEAPON].Pressed and AnyServer then PrevWeapon();
4305 if gTime mod (GAME_TICK*2) <> 0 then
4306 begin
4307 if (FObj.Vel.X = 0) and FAlive then
4308 begin
4309 if FKeys[KEY_LEFT].Pressed then
4310 Run(TDirection.D_LEFT);
4311 if FKeys[KEY_RIGHT].Pressed then
4312 Run(TDirection.D_RIGHT);
4313 end;
4315 if FPhysics then
4316 begin
4317 g_Obj_Move(@FObj, True, True, True);
4318 positionChanged(); // this updates spatial accelerators
4319 end;
4321 Exit;
4322 end;
4324 FActionChanged := False;
4326 if FAlive then
4327 begin
4328 // Let alive player do some actions
4329 if FKeys[KEY_LEFT].Pressed then Run(TDirection.D_LEFT);
4330 if FKeys[KEY_RIGHT].Pressed then Run(TDirection.D_RIGHT);
4331 //if FKeys[KEY_NEXTWEAPON].Pressed and AnyServer then NextWeapon();
4332 //if FKeys[KEY_PREVWEAPON].Pressed and AnyServer then PrevWeapon();
4333 if FKeys[KEY_FIRE].Pressed and AnyServer then Fire()
4334 else
4335 begin
4336 if AnyServer then
4337 begin
4338 FlamerOff;
4339 if NetServer then MH_SEND_PlayerStats(FUID);
4340 end;
4341 end;
4342 if FKeys[KEY_OPEN].Pressed and AnyServer then Use();
4343 if FKeys[KEY_JUMP].Pressed then Jump()
4344 else
4345 begin
4346 if AnyServer and FJetpack then
4347 begin
4348 FJetpack := False;
4349 JetpackOff;
4350 if NetServer then MH_SEND_PlayerStats(FUID);
4351 end;
4352 FCanJetpack := True;
4353 end;
4354 end
4355 else // Dead
4356 begin
4357 dospawn := False;
4358 if not FGhost then
4359 for k := Low(FKeys) to KEY_CHAT-1 do
4360 begin
4361 if FKeys[k].Pressed then
4362 begin
4363 dospawn := True;
4364 break;
4365 end;
4366 end;
4367 if dospawn then
4368 begin
4369 if gGameSettings.GameType in [GT_CUSTOM, GT_SERVER, GT_CLIENT] then
4370 Respawn(False)
4371 else // Single
4372 if (FTime[T_RESPAWN] <= gTime) and
4373 gGameOn and (not FAlive) then
4374 begin
4375 if (g_Player_GetCount() > 1) then
4376 Respawn(False)
4377 else
4378 begin
4379 gExit := EXIT_RESTART;
4380 Exit;
4381 end;
4382 end;
4383 end;
4384 // Dead spectator actions
4385 if FGhost then
4386 begin
4387 if FKeys[KEY_OPEN].Pressed and AnyServer then Fire();
4388 if FKeys[KEY_FIRE].Pressed and AnyServer then
4389 begin
4390 if FSpectator then
4391 begin
4392 if (FSpectatePlayer >= High(gPlayers)) then
4393 FSpectatePlayer := -1
4394 else
4395 begin
4396 SetSpect := False;
4397 for I := FSpectatePlayer + 1 to High(gPlayers) do
4398 if gPlayers[I] <> nil then
4399 if gPlayers[I].alive then
4400 if gPlayers[I].UID <> FUID then
4401 begin
4402 FSpectatePlayer := I;
4403 SetSpect := True;
4404 break;
4405 end;
4407 if not SetSpect then FSpectatePlayer := -1;
4408 end;
4410 ReleaseKeys;
4411 end;
4412 end;
4413 end;
4414 end;
4415 // No clipping
4416 if FGhost then
4417 begin
4418 if FKeys[KEY_UP].Pressed or FKeys[KEY_JUMP].Pressed then
4419 begin
4420 FYTo := FObj.Y - 32;
4421 FSpectatePlayer := -1;
4422 end;
4423 if FKeys[KEY_DOWN].Pressed then
4424 begin
4425 FYTo := FObj.Y + 32;
4426 FSpectatePlayer := -1;
4427 end;
4428 if FKeys[KEY_LEFT].Pressed then
4429 begin
4430 FXTo := FObj.X - 32;
4431 FSpectatePlayer := -1;
4432 end;
4433 if FKeys[KEY_RIGHT].Pressed then
4434 begin
4435 FXTo := FObj.X + 32;
4436 FSpectatePlayer := -1;
4437 end;
4439 if (FXTo < -64) then
4440 FXTo := -64
4441 else if (FXTo > gMapInfo.Width + 32) then
4442 FXTo := gMapInfo.Width + 32;
4443 if (FYTo < -72) then
4444 FYTo := -72
4445 else if (FYTo > gMapInfo.Height + 32) then
4446 FYTo := gMapInfo.Height + 32;
4447 end;
4449 if FPhysics then
4450 begin
4451 g_Obj_Move(@FObj, True, True, True);
4452 positionChanged(); // this updates spatial accelerators
4453 end
4454 else
4455 begin
4456 FObj.Vel.X := 0;
4457 FObj.Vel.Y := 0;
4458 if FSpectator then
4459 if (FSpectatePlayer <= High(gPlayers)) and (FSpectatePlayer >= 0) then
4460 if gPlayers[FSpectatePlayer] <> nil then
4461 if gPlayers[FSpectatePlayer].alive then
4462 begin
4463 FXTo := gPlayers[FSpectatePlayer].GameX;
4464 FYTo := gPlayers[FSpectatePlayer].GameY;
4465 end;
4466 end;
4468 blockmon := g_Map_CollidePanel(FObj.X+PLAYER_HEADRECT.X, FObj.Y+PLAYER_HEADRECT.Y,
4469 PLAYER_HEADRECT.Width, PLAYER_HEADRECT.Height,
4470 PANEL_BLOCKMON, True);
4471 headwater := HeadInLiquid(0, 0);
4473 // Сопротивление воздуха:
4474 if (not FAlive) or not (FKeys[KEY_LEFT].Pressed or FKeys[KEY_RIGHT].Pressed) then
4475 if FObj.Vel.X <> 0 then
4476 FObj.Vel.X := z_dec(FObj.Vel.X, 1);
4478 if (FLastHit = HIT_TRAP) and (FPain > 90) then FPain := 90;
4479 DecMin(FPain, 5, 0);
4480 DecMin(FPickup, 1, 0);
4482 if FAlive and (FObj.Y > Integer(gMapInfo.Height)+128) and AnyServer then
4483 begin
4484 // Обнулить действия примочек, чтобы фон пропал
4485 FMegaRulez[MR_SUIT] := 0;
4486 FMegaRulez[MR_INVUL] := 0;
4487 FMegaRulez[MR_INVIS] := 0;
4488 Kill(K_FALLKILL, 0, HIT_FALL);
4489 end;
4491 i := 9;
4493 if FAlive then
4494 begin
4495 if FCurrWeap = WEAPON_SAW then
4496 if not (FSawSound.IsPlaying() or FSawSoundHit.IsPlaying() or
4497 FSawSoundSelect.IsPlaying()) then
4498 FSawSoundIdle.PlayAt(FObj.X, FObj.Y);
4500 if FJetpack then
4501 if (not FJetSoundFly.IsPlaying()) and (not FJetSoundOn.IsPlaying()) and
4502 (not FJetSoundOff.IsPlaying()) then
4503 begin
4504 FJetSoundFly.SetPosition(0);
4505 FJetSoundFly.PlayAt(FObj.X, FObj.Y);
4506 end;
4508 for b := WP_FIRST to WP_LAST do
4509 if FReloading[b] > 0 then
4510 if FNoReload then
4511 FReloading[b] := 0
4512 else
4513 Dec(FReloading[b]);
4515 if FShellTimer > -1 then
4516 if FShellTimer = 0 then
4517 begin
4518 if FShellType = SHELL_SHELL then
4519 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4520 GameVelX, GameVelY-2, SHELL_SHELL)
4521 else if FShellType = SHELL_DBLSHELL then
4522 begin
4523 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4524 GameVelX+1, GameVelY-2, SHELL_SHELL);
4525 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4526 GameVelX-1, GameVelY-2, SHELL_SHELL);
4527 end;
4528 FShellTimer := -1;
4529 end else Dec(FShellTimer);
4531 if (FBFGFireCounter > -1) then
4532 if FBFGFireCounter = 0 then
4533 begin
4534 if AnyServer then
4535 begin
4536 wx := FObj.X+WEAPONPOINT[FDirection].X;
4537 wy := FObj.Y+WEAPONPOINT[FDirection].Y;
4538 xd := wx+IfThen(FDirection = TDirection.D_LEFT, -30, 30);
4539 yd := wy+firediry();
4540 g_Weapon_bfgshot(wx, wy, xd, yd, FUID);
4541 if NetServer then MH_SEND_PlayerFire(FUID, WEAPON_BFG, wx, wy, xd, yd);
4542 if (FAngle = 0) or (FAngle = 180) then SetAction(A_ATTACK)
4543 else if (FAngle = ANGLE_LEFTDOWN) or (FAngle = ANGLE_RIGHTDOWN) then SetAction(A_ATTACKDOWN)
4544 else if (FAngle = ANGLE_LEFTUP) or (FAngle = ANGLE_RIGHTUP) then SetAction(A_ATTACKUP);
4545 end;
4547 FReloading[WEAPON_BFG] := WEAPON_RELOAD[WEAPON_BFG];
4548 FBFGFireCounter := -1;
4549 end else
4550 if FNoReload then
4551 FBFGFireCounter := 0
4552 else
4553 Dec(FBFGFireCounter);
4555 if (FMegaRulez[MR_SUIT] < gTime) and AnyServer then
4556 begin
4557 b := g_GetAcidHit(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y, PLAYER_RECT.Width, PLAYER_RECT.Height);
4559 if (b > 0) and (gTime mod (15*GAME_TICK) = 0) then Damage(b, 0, 0, 0, HIT_ACID);
4560 end;
4562 if (headwater or blockmon) then
4563 begin
4564 Dec(FAir);
4566 if FAir < -9 then
4567 begin
4568 if AnyServer then Damage(10, 0, 0, 0, HIT_WATER);
4569 FAir := 0;
4570 end
4571 else if (FAir mod 31 = 0) and not blockmon then
4572 begin
4573 g_GFX_Bubbles(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2), FObj.Y+PLAYER_RECT.Y-4, 5+Random(6), 8, 4);
4574 if Random(2) = 0 then
4575 g_Sound_PlayExAt('SOUND_GAME_BUBBLE1', FObj.X, FObj.Y)
4576 else
4577 g_Sound_PlayExAt('SOUND_GAME_BUBBLE2', FObj.X, FObj.Y);
4578 end;
4579 end else if FAir < AIR_DEF then
4580 FAir := AIR_DEF;
4582 if FFireTime > 0 then
4583 begin
4584 if BodyInLiquid(0, 0) then
4585 begin
4586 FFireTime := 0;
4587 FFirePainTime := 0;
4588 end
4589 else if FMegaRulez[MR_SUIT] >= gTime then
4590 begin
4591 if FMegaRulez[MR_SUIT] = gTime then
4592 FFireTime := 1;
4593 FFirePainTime := 0;
4594 end
4595 else
4596 begin
4597 OnFireFlame(1);
4598 if FFirePainTime <= 0 then
4599 begin
4600 if g_Game_IsServer then
4601 Damage(2, FFireAttacker, 0, 0, HIT_FLAME);
4602 FFirePainTime := 12 - FFireTime div 12;
4603 end;
4604 FFirePainTime := FFirePainTime - 1;
4605 FFireTime := FFireTime - 1;
4606 if ((FFireTime mod 33) = 0) and (FMegaRulez[MR_INVUL] < gTime) then
4607 FModel.PlaySound(MODELSOUND_PAIN, 1, FObj.X, FObj.Y);
4608 if (FFireTime = 0) and g_Game_IsNet and g_Game_IsServer then
4609 MH_SEND_PlayerStats(FUID);
4610 end;
4611 end;
4613 if FDamageBuffer > 0 then
4614 begin
4615 if FDamageBuffer >= 9 then
4616 begin
4617 SetAction(A_PAIN);
4619 if FDamageBuffer < 30 then i := 9
4620 else if FDamageBuffer < 100 then i := 18
4621 else i := 27;
4622 end;
4624 ii := Round(FDamageBuffer*FHealth / nonz(FArmor*(3/4)+FHealth));
4625 FArmor := FArmor-(FDamageBuffer-ii);
4626 FHealth := FHealth-ii;
4627 if FArmor < 0 then
4628 begin
4629 FHealth := FHealth+FArmor;
4630 FArmor := 0;
4631 end;
4633 if AnyServer then
4634 if FHealth <= 0 then
4635 if FHealth > -30 then Kill(K_SIMPLEKILL, FLastSpawnerUID, FLastHit)
4636 else if FHealth > -50 then Kill(K_HARDKILL, FLastSpawnerUID, FLastHit)
4637 else Kill(K_EXTRAHARDKILL, FLastSpawnerUID, FLastHit);
4639 if FAlive and ((FLastHit <> HIT_FLAME) or (FFireTime <= 0)) then
4640 begin
4641 if FDamageBuffer <= 20 then FModel.PlaySound(MODELSOUND_PAIN, 1, FObj.X, FObj.Y)
4642 else if FDamageBuffer <= 55 then FModel.PlaySound(MODELSOUND_PAIN, 2, FObj.X, FObj.Y)
4643 else if FDamageBuffer <= 120 then FModel.PlaySound(MODELSOUND_PAIN, 3, FObj.X, FObj.Y)
4644 else FModel.PlaySound(MODELSOUND_PAIN, 4, FObj.X, FObj.Y);
4645 end;
4647 FDamageBuffer := 0;
4648 end;
4650 {CollideItem();}
4651 end; // if FAlive then ...
4653 if (FActionAnim = A_PAIN) and (FModel.Animation <> A_PAIN) then
4654 begin
4655 FModel.ChangeAnimation(FActionAnim, FActionForce);
4656 FModel.AnimState.MinLength := i;
4657 end else FModel.ChangeAnimation(FActionAnim, FActionForce and (FModel.Animation <> A_STAND));
4659 if (FModel.AnimState.Played or ((not FActionChanged) and (FModel.Animation = A_WALK)))
4660 then SetAction(A_STAND, True);
4662 if not ((FModel.Animation = A_WALK) and (Abs(FObj.Vel.X) < 4) and not FModel.GetFire()) then FModel.Update;
4664 for b := Low(FKeys) to High(FKeys) do
4665 if FKeys[b].Time = 0 then FKeys[b].Pressed := False else Dec(FKeys[b].Time);
4666 end;
4669 procedure TPlayer.getMapBox (out x, y, w, h: Integer); inline;
4670 begin
4671 x := FObj.X+PLAYER_RECT.X;
4672 y := FObj.Y+PLAYER_RECT.Y;
4673 w := PLAYER_RECT.Width;
4674 h := PLAYER_RECT.Height;
4675 end;
4678 procedure TPlayer.moveBy (dx, dy: Integer); inline;
4679 begin
4680 if (dx <> 0) or (dy <> 0) then
4681 begin
4682 FObj.X += dx;
4683 FObj.Y += dy;
4684 positionChanged();
4685 end;
4686 end;
4689 function TPlayer.Collide(X, Y: Integer; Width, Height: Word): Boolean;
4690 begin
4691 Result := g_Collide(FObj.X+PLAYER_RECT.X,
4692 FObj.Y+PLAYER_RECT.Y,
4693 PLAYER_RECT.Width,
4694 PLAYER_RECT.Height,
4695 X, Y,
4696 Width, Height);
4697 end;
4699 function TPlayer.Collide(Panel: TPanel): Boolean;
4700 begin
4701 Result := g_Collide(FObj.X+PLAYER_RECT.X,
4702 FObj.Y+PLAYER_RECT.Y,
4703 PLAYER_RECT.Width,
4704 PLAYER_RECT.Height,
4705 Panel.X, Panel.Y,
4706 Panel.Width, Panel.Height);
4707 end;
4709 function TPlayer.Collide(X, Y: Integer): Boolean;
4710 begin
4711 X := X-FObj.X-PLAYER_RECT.X;
4712 Y := Y-FObj.Y-PLAYER_RECT.Y;
4713 Result := (x >= 0) and (x <= PLAYER_RECT.Width) and
4714 (y >= 0) and (y <= PLAYER_RECT.Height);
4715 end;
4717 function g_Player_ValidName(Name: string): Boolean;
4718 var
4719 a: Integer;
4720 begin
4721 Result := True;
4723 if gPlayers = nil then Exit;
4725 for a := 0 to High(gPlayers) do
4726 if gPlayers[a] <> nil then
4727 if LowerCase(Name) = LowerCase(gPlayers[a].FName) then
4728 begin
4729 Result := False;
4730 Exit;
4731 end;
4732 end;
4734 procedure TPlayer.SetDirection(Direction: TDirection);
4735 var
4736 d: TDirection;
4737 begin
4738 d := FModel.Direction;
4740 FModel.Direction := Direction;
4741 if d <> Direction then FModel.ChangeAnimation(FModel.Animation, True);
4743 FDirection := Direction;
4744 end;
4746 function TPlayer.GetKeys(): Byte;
4747 begin
4748 Result := 0;
4750 if R_KEY_RED in FRulez then Result := KEY_RED;
4751 if R_KEY_GREEN in FRulez then Result := Result or KEY_GREEN;
4752 if R_KEY_BLUE in FRulez then Result := Result or KEY_BLUE;
4754 if FTeam = TEAM_RED then Result := Result or KEY_REDTEAM;
4755 if FTeam = TEAM_BLUE then Result := Result or KEY_BLUETEAM;
4756 end;
4758 procedure TPlayer.Use();
4759 var
4760 a: Integer;
4761 begin
4762 if FTime[T_USE] > gTime then Exit;
4764 g_Triggers_PressR(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y, PLAYER_RECT.Width,
4765 PLAYER_RECT.Height, FUID, ACTIVATE_PLAYERPRESS);
4767 for a := 0 to High(gPlayers) do
4768 if (gPlayers[a] <> nil) and (gPlayers[a] <> Self) and
4769 gPlayers[a].alive and SameTeam(FUID, gPlayers[a].FUID) and
4770 g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y,
4771 FObj.Rect.Width, FObj.Rect.Height, @gPlayers[a].FObj) then
4772 begin
4773 gPlayers[a].Touch();
4774 if g_Game_IsNet and g_Game_IsServer then
4775 MH_SEND_GameEvent(NET_EV_PLAYER_TOUCH, gPlayers[a].FUID);
4776 end;
4778 FTime[T_USE] := gTime+120;
4779 end;
4781 procedure TPlayer.NetFire(Wpn: Byte; X, Y, AX, AY: Integer; WID: Integer = -1);
4782 var
4783 locObj: TObj;
4784 F: Boolean;
4785 WX, WY, XD, YD: Integer;
4786 begin
4787 F := False;
4788 WX := X;
4789 WY := Y;
4790 XD := AX;
4791 YD := AY;
4793 case FCurrWeap of
4794 WEAPON_KASTET:
4795 begin
4796 DoPunch();
4797 if R_BERSERK in FRulez then
4798 begin
4799 //g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 75, FUID);
4800 locobj.X := FObj.X+FObj.Rect.X;
4801 locobj.Y := FObj.Y+FObj.Rect.Y;
4802 locobj.rect.X := 0;
4803 locobj.rect.Y := 0;
4804 locobj.rect.Width := 39;
4805 locobj.rect.Height := 52;
4806 locobj.Vel.X := (xd-wx) div 2;
4807 locobj.Vel.Y := (yd-wy) div 2;
4808 locobj.Accel.X := xd-wx;
4809 locobj.Accel.y := yd-wy;
4811 if g_Weapon_Hit(@locobj, 50, FUID, HIT_SOME) <> 0 then
4812 g_Sound_PlayExAt('SOUND_WEAPON_HITBERSERK', FObj.X, FObj.Y)
4813 else
4814 g_Sound_PlayExAt('SOUND_WEAPON_MISSBERSERK', FObj.X, FObj.Y);
4816 if gFlash = 1 then
4817 if FPain < 50 then
4818 FPain := min(FPain + 25, 50);
4819 end else
4820 g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 3, FUID);
4821 end;
4823 WEAPON_SAW:
4824 begin
4825 if g_Weapon_chainsaw(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y,
4826 IfThen(gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF], 9, 3), FUID) <> 0 then
4827 begin
4828 FSawSoundSelect.Stop();
4829 FSawSound.Stop();
4830 FSawSoundHit.PlayAt(FObj.X, FObj.Y);
4831 end
4832 else if not FSawSoundHit.IsPlaying() then
4833 begin
4834 FSawSoundSelect.Stop();
4835 FSawSound.PlayAt(FObj.X, FObj.Y);
4836 end;
4837 f := True;
4838 end;
4840 WEAPON_PISTOL:
4841 begin
4842 g_Sound_PlayExAt('SOUND_WEAPON_FIREPISTOL', GameX, Gamey);
4843 FFireAngle := FAngle;
4844 f := True;
4845 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4846 GameVelX, GameVelY-2, SHELL_BULLET);
4847 end;
4849 WEAPON_SHOTGUN1:
4850 begin
4851 g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN', Gamex, Gamey);
4852 FFireAngle := FAngle;
4853 f := True;
4854 FShellTimer := 10;
4855 FShellType := SHELL_SHELL;
4856 end;
4858 WEAPON_SHOTGUN2:
4859 begin
4860 g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN2', Gamex, Gamey);
4861 FFireAngle := FAngle;
4862 f := True;
4863 FShellTimer := 13;
4864 FShellType := SHELL_DBLSHELL;
4865 end;
4867 WEAPON_CHAINGUN:
4868 begin
4869 g_Sound_PlayExAt('SOUND_WEAPON_FIRECGUN', Gamex, Gamey);
4870 FFireAngle := FAngle;
4871 f := True;
4872 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4873 GameVelX, GameVelY-2, SHELL_BULLET);
4874 end;
4876 WEAPON_ROCKETLAUNCHER:
4877 begin
4878 g_Weapon_Rocket(wx, wy, xd, yd, FUID, WID);
4879 FFireAngle := FAngle;
4880 f := True;
4881 end;
4883 WEAPON_PLASMA:
4884 begin
4885 g_Weapon_Plasma(wx, wy, xd, yd, FUID, WID);
4886 FFireAngle := FAngle;
4887 f := True;
4888 end;
4890 WEAPON_BFG:
4891 begin
4892 g_Weapon_BFGShot(wx, wy, xd, yd, FUID, WID);
4893 FFireAngle := FAngle;
4894 f := True;
4895 end;
4897 WEAPON_SUPERPULEMET:
4898 begin
4899 g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN', Gamex, Gamey);
4900 FFireAngle := FAngle;
4901 f := True;
4902 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4903 GameVelX, GameVelY-2, SHELL_SHELL);
4904 end;
4906 WEAPON_FLAMETHROWER:
4907 begin
4908 g_Weapon_flame(wx, wy, xd, yd, FUID, WID);
4909 FlamerOn;
4910 FFireAngle := FAngle;
4911 f := True;
4912 end;
4913 end;
4915 if not f then Exit;
4917 if (FAngle = 0) or (FAngle = 180) then SetAction(A_ATTACK)
4918 else if (FAngle = ANGLE_LEFTDOWN) or (FAngle = ANGLE_RIGHTDOWN) then SetAction(A_ATTACKDOWN)
4919 else if (FAngle = ANGLE_LEFTUP) or (FAngle = ANGLE_RIGHTUP) then SetAction(A_ATTACKUP);
4920 end;
4922 procedure TPlayer.DoLerp(Level: Integer = 2);
4923 begin
4924 if FObj.X <> FXTo then FObj.X := Lerp(FObj.X, FXTo, Level);
4925 if FObj.Y <> FYTo then FObj.Y := Lerp(FObj.Y, FYTo, Level);
4926 end;
4928 procedure TPlayer.SetLerp(XTo, YTo: Integer);
4929 var
4930 AX, AY: Integer;
4931 begin
4932 FXTo := XTo;
4933 FYTo := YTo;
4934 if FJustTeleported or (NetInterpLevel < 1) then
4935 begin
4936 FObj.X := XTo;
4937 FObj.Y := YTo;
4938 if FJustTeleported then
4939 begin
4940 FObj.oldX := FObj.X;
4941 FObj.oldY := FObj.Y;
4942 end;
4943 end
4944 else
4945 begin
4946 AX := Abs(FXTo - FObj.X);
4947 AY := Abs(FYTo - FObj.Y);
4948 if (AX > 32) or (AX <= NetInterpLevel) then
4949 FObj.X := FXTo;
4950 if (AY > 32) or (AY <= NetInterpLevel) then
4951 FObj.Y := FYTo;
4952 end;
4953 end;
4955 function TPlayer.FullInLift(XInc, YInc: Integer): Integer;
4956 begin
4957 if g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
4958 PLAYER_RECT.Width, PLAYER_RECT.Height-8,
4959 PANEL_LIFTUP, False) then Result := -1
4960 else
4961 if g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
4962 PLAYER_RECT.Width, PLAYER_RECT.Height-8,
4963 PANEL_LIFTDOWN, False) then Result := 1
4964 else Result := 0;
4965 end;
4967 function TPlayer.GetFlag(Flag: Byte): Boolean;
4968 var
4969 s, ts: String;
4970 evtype, a: Byte;
4971 begin
4972 Result := False;
4974 if Flag = FLAG_NONE then
4975 Exit;
4977 if not g_Game_IsServer then Exit;
4979 // Принес чужой флаг на свою базу:
4980 if (Flag = FTeam) and
4981 (gFlags[Flag].State = FLAG_STATE_NORMAL) and
4982 (FFlag <> FLAG_NONE) then
4983 begin
4984 if FFlag = FLAG_RED then
4985 s := _lc[I_PLAYER_FLAG_RED]
4986 else
4987 s := _lc[I_PLAYER_FLAG_BLUE];
4989 evtype := FLAG_STATE_SCORED;
4991 ts := Format('%.4d', [gFlags[FFlag].CaptureTime]);
4992 Insert('.', ts, Length(ts) + 1 - 3);
4993 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [FName, s, ts]), True);
4995 g_Map_ResetFlag(FFlag);
4996 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
4998 if ((Self = gPlayer1) or (Self = gPlayer2)
4999 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5000 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5001 a := 0
5002 else
5003 a := 1;
5005 if not sound_cap_flag[a].IsPlaying() then
5006 sound_cap_flag[a].Play();
5008 gTeamStat[FTeam].Goals := gTeamStat[FTeam].Goals + 1;
5010 Result := True;
5011 if g_Game_IsNet then
5012 begin
5013 MH_SEND_FlagEvent(evtype, FFlag, FUID, False);
5014 MH_SEND_GameStats;
5015 end;
5017 gFlags[FFlag].CaptureTime := 0;
5018 SetFlag(FLAG_NONE);
5019 Exit;
5020 end;
5022 // Подобрал свой флаг - вернул его на базу:
5023 if (Flag = FTeam) and
5024 (gFlags[Flag].State = FLAG_STATE_DROPPED) then
5025 begin
5026 if Flag = FLAG_RED then
5027 s := _lc[I_PLAYER_FLAG_RED]
5028 else
5029 s := _lc[I_PLAYER_FLAG_BLUE];
5031 evtype := FLAG_STATE_RETURNED;
5032 gFlags[Flag].CaptureTime := 0;
5034 g_Console_Add(Format(_lc[I_PLAYER_FLAG_RETURN], [FName, s]), True);
5036 g_Map_ResetFlag(Flag);
5037 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
5039 if ((Self = gPlayer1) or (Self = gPlayer2)
5040 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5041 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5042 a := 0
5043 else
5044 a := 1;
5046 if not sound_ret_flag[a].IsPlaying() then
5047 sound_ret_flag[a].Play();
5049 Result := True;
5050 if g_Game_IsNet then
5051 begin
5052 MH_SEND_FlagEvent(evtype, Flag, FUID, False);
5053 MH_SEND_GameStats;
5054 end;
5055 Exit;
5056 end;
5058 // Подобрал чужой флаг:
5059 if (Flag <> FTeam) and (FTime[T_FLAGCAP] <= gTime) then
5060 begin
5061 SetFlag(Flag);
5063 if Flag = FLAG_RED then
5064 s := _lc[I_PLAYER_FLAG_RED]
5065 else
5066 s := _lc[I_PLAYER_FLAG_BLUE];
5068 evtype := FLAG_STATE_CAPTURED;
5070 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [FName, s]), True);
5072 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
5074 gFlags[Flag].State := FLAG_STATE_CAPTURED;
5076 if ((Self = gPlayer1) or (Self = gPlayer2)
5077 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5078 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5079 a := 0
5080 else
5081 a := 1;
5083 if not sound_get_flag[a].IsPlaying() then
5084 sound_get_flag[a].Play();
5086 Result := True;
5087 if g_Game_IsNet then
5088 begin
5089 MH_SEND_FlagEvent(evtype, Flag, FUID, False);
5090 MH_SEND_GameStats;
5091 end;
5092 end;
5093 end;
5095 procedure TPlayer.SetFlag(Flag: Byte);
5096 begin
5097 FFlag := Flag;
5098 if FModel <> nil then
5099 FModel.SetFlag(FFlag);
5100 end;
5102 function TPlayer.DropFlag(Silent: Boolean = True): Boolean;
5103 var
5104 s: String;
5105 a: Byte;
5106 begin
5107 Result := False;
5108 if (not g_Game_IsServer) or (FFlag = FLAG_NONE) then
5109 Exit;
5110 FTime[T_FLAGCAP] := gTime + 2000;
5111 with gFlags[FFlag] do
5112 begin
5113 Obj.X := FObj.X;
5114 Obj.Y := FObj.Y;
5115 Direction := FDirection;
5116 State := FLAG_STATE_DROPPED;
5117 Count := FLAG_TIME;
5118 g_Obj_Push(@Obj, (FObj.Vel.X div 2)-2+Random(5),
5119 (FObj.Vel.Y div 2)-2+Random(5));
5120 positionChanged(); // this updates spatial accelerators
5122 if FFlag = FLAG_RED then
5123 s := _lc[I_PLAYER_FLAG_RED]
5124 else
5125 s := _lc[I_PLAYER_FLAG_BLUE];
5127 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [FName, s]), True);
5128 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
5130 if ((Self = gPlayer1) or (Self = gPlayer2)
5131 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5132 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5133 a := 0
5134 else
5135 a := 1;
5137 if (not Silent) and (not sound_lost_flag[a].IsPlaying()) then
5138 sound_lost_flag[a].Play();
5140 if g_Game_IsNet then
5141 MH_SEND_FlagEvent(FLAG_STATE_DROPPED, Flag, FUID, False);
5142 end;
5143 SetFlag(FLAG_NONE);
5144 Result := True;
5145 end;
5147 procedure TPlayer.GetSecret();
5148 begin
5149 if (self = gPlayer1) or (self = gPlayer2) then
5150 begin
5151 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [FName]), True);
5152 g_Sound_PlayEx('SOUND_GAME_SECRET');
5153 end;
5154 Inc(FSecrets);
5155 end;
5157 procedure TPlayer.PressKey(Key: Byte; Time: Word = 1);
5158 begin
5159 Assert(Key <= High(FKeys));
5161 FKeys[Key].Pressed := True;
5162 FKeys[Key].Time := Time;
5163 end;
5165 function TPlayer.IsKeyPressed(K: Byte): Boolean;
5166 begin
5167 Result := FKeys[K].Pressed;
5168 end;
5170 procedure TPlayer.ReleaseKeys();
5171 var
5172 a: Integer;
5173 begin
5174 for a := Low(FKeys) to High(FKeys) do
5175 begin
5176 FKeys[a].Pressed := False;
5177 FKeys[a].Time := 0;
5178 end;
5179 end;
5181 procedure TPlayer.OnDamage(Angle: SmallInt);
5182 begin
5183 end;
5185 function TPlayer.firediry(): Integer;
5186 begin
5187 if FKeys[KEY_UP].Pressed then Result := -42
5188 else if FKeys[KEY_DOWN].Pressed then Result := 19
5189 else Result := 0;
5190 end;
5192 procedure TPlayer.RememberState();
5193 var
5194 i: Integer;
5195 SavedState: TPlayerSavedState;
5196 begin
5197 SavedState.Health := FHealth;
5198 SavedState.Armor := FArmor;
5199 SavedState.Air := FAir;
5200 SavedState.JetFuel := FJetFuel;
5201 SavedState.CurrWeap := FCurrWeap;
5202 SavedState.NextWeap := FNextWeap;
5203 SavedState.NextWeapDelay := FNextWeapDelay;
5204 for i := Low(FWeapon) to High(FWeapon) do
5205 SavedState.Weapon[i] := FWeapon[i];
5206 for i := Low(FAmmo) to High(FAmmo) do
5207 SavedState.Ammo[i] := FAmmo[i];
5208 for i := Low(FMaxAmmo) to High(FMaxAmmo) do
5209 SavedState.MaxAmmo[i] := FMaxAmmo[i];
5210 SavedState.Rulez := FRulez - [R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE];
5212 FSavedStateNum := -1;
5213 for i := Low(SavedStates) to High(SavedStates) do
5214 if not SavedStates[i].Used then
5215 begin
5216 FSavedStateNum := i;
5217 break;
5218 end;
5219 if FSavedStateNum < 0 then
5220 begin
5221 SetLength(SavedStates, Length(SavedStates) + 1);
5222 FSavedStateNum := High(SavedStates);
5223 end;
5225 SavedState.Used := True;
5226 SavedStates[FSavedStateNum] := SavedState;
5227 end;
5229 procedure TPlayer.RecallState();
5230 var
5231 i: Integer;
5232 SavedState: TPlayerSavedState;
5233 begin
5234 if(FSavedStateNum < 0) or (FSavedStateNum > High(SavedStates)) then
5235 Exit;
5237 SavedState := SavedStates[FSavedStateNum];
5238 SavedStates[FSavedStateNum].Used := False;
5239 FSavedStateNum := -1;
5241 FHealth := SavedState.Health;
5242 FArmor := SavedState.Armor;
5243 FAir := SavedState.Air;
5244 FJetFuel := SavedState.JetFuel;
5245 FCurrWeap := SavedState.CurrWeap;
5246 FNextWeap := SavedState.NextWeap;
5247 FNextWeapDelay := SavedState.NextWeapDelay;
5248 for i := Low(FWeapon) to High(FWeapon) do
5249 FWeapon[i] := SavedState.Weapon[i];
5250 for i := Low(FAmmo) to High(FAmmo) do
5251 FAmmo[i] := SavedState.Ammo[i];
5252 for i := Low(FMaxAmmo) to High(FMaxAmmo) do
5253 FMaxAmmo[i] := SavedState.MaxAmmo[i];
5254 FRulez := SavedState.Rulez;
5256 if gGameSettings.GameType = GT_SERVER then
5257 MH_SEND_PlayerStats(FUID);
5258 end;
5260 procedure TPlayer.SaveState (st: TStream);
5261 var
5262 i: Integer;
5263 b: Byte;
5264 begin
5265 // Сигнатура игрока
5266 utils.writeSign(st, 'PLYR');
5267 utils.writeInt(st, Byte(PLR_SAVE_VERSION)); // version
5268 // Бот или человек
5269 utils.writeBool(st, FIamBot);
5270 // UID игрока
5271 utils.writeInt(st, Word(FUID));
5272 // Имя игрока
5273 utils.writeStr(st, FName);
5274 // Команда
5275 utils.writeInt(st, Byte(FTeam));
5276 // Жив ли
5277 utils.writeBool(st, FAlive);
5278 // Израсходовал ли все жизни
5279 utils.writeBool(st, FNoRespawn);
5280 // Направление
5281 if FDirection = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
5282 utils.writeInt(st, Byte(b));
5283 // Здоровье
5284 utils.writeInt(st, LongInt(FHealth));
5285 // Коэффициент инвалидности
5286 utils.writeInt(st, LongInt(FHandicap));
5287 // Жизни
5288 utils.writeInt(st, Byte(FLives));
5289 // Броня
5290 utils.writeInt(st, LongInt(FArmor));
5291 // Запас воздуха
5292 utils.writeInt(st, LongInt(FAir));
5293 // Запас горючего
5294 utils.writeInt(st, LongInt(FJetFuel));
5295 // Боль
5296 utils.writeInt(st, LongInt(FPain));
5297 // Убил
5298 utils.writeInt(st, LongInt(FKills));
5299 // Убил монстров
5300 utils.writeInt(st, LongInt(FMonsterKills));
5301 // Фрагов
5302 utils.writeInt(st, LongInt(FFrags));
5303 // Фрагов подряд
5304 utils.writeInt(st, Byte(FFragCombo));
5305 // Время последнего фрага
5306 utils.writeInt(st, LongWord(FLastFrag));
5307 // Смертей
5308 utils.writeInt(st, LongInt(FDeath));
5309 // Какой флаг несет
5310 utils.writeInt(st, Byte(FFlag));
5311 // Нашел секретов
5312 utils.writeInt(st, LongInt(FSecrets));
5313 // Текущее оружие
5314 utils.writeInt(st, Byte(FCurrWeap));
5315 // Желаемое оружие
5316 utils.writeInt(st, Word(FNextWeap));
5317 // ...и пауза
5318 utils.writeInt(st, Byte(FNextWeapDelay));
5319 // Время зарядки BFG
5320 utils.writeInt(st, SmallInt(FBFGFireCounter));
5321 // Буфер урона
5322 utils.writeInt(st, LongInt(FDamageBuffer));
5323 // Последний ударивший
5324 utils.writeInt(st, Word(FLastSpawnerUID));
5325 // Тип последнего полученного урона
5326 utils.writeInt(st, Byte(FLastHit));
5327 // Объект игрока
5328 Obj_SaveState(st, @FObj);
5329 // Текущее количество патронов
5330 for i := A_BULLETS to A_HIGH do utils.writeInt(st, Word(FAmmo[i]));
5331 // Максимальное количество патронов
5332 for i := A_BULLETS to A_HIGH do utils.writeInt(st, Word(FMaxAmmo[i]));
5333 // Наличие оружия
5334 for i := WP_FIRST to WP_LAST do utils.writeBool(st, FWeapon[i]);
5335 // Время перезарядки оружия
5336 for i := WP_FIRST to WP_LAST do utils.writeInt(st, Word(FReloading[i]));
5337 // Наличие рюкзака
5338 utils.writeBool(st, (R_ITEM_BACKPACK in FRulez));
5339 // Наличие красного ключа
5340 utils.writeBool(st, (R_KEY_RED in FRulez));
5341 // Наличие зеленого ключа
5342 utils.writeBool(st, (R_KEY_GREEN in FRulez));
5343 // Наличие синего ключа
5344 utils.writeBool(st, (R_KEY_BLUE in FRulez));
5345 // Наличие берсерка
5346 utils.writeBool(st, (R_BERSERK in FRulez));
5347 // Время действия специальных предметов
5348 for i := MR_SUIT to MR_MAX do utils.writeInt(st, LongWord(FMegaRulez[i]));
5349 // Время до повторного респауна, смены оружия, исользования, захвата флага
5350 for i := T_RESPAWN to T_FLAGCAP do utils.writeInt(st, LongWord(FTime[i]));
5351 // Название модели
5352 utils.writeStr(st, FModel.GetName());
5353 // Цвет модели
5354 utils.writeInt(st, Byte(FColor.R));
5355 utils.writeInt(st, Byte(FColor.G));
5356 utils.writeInt(st, Byte(FColor.B));
5357 end;
5360 procedure TPlayer.LoadState (st: TStream);
5361 var
5362 i: Integer;
5363 str: String;
5364 b: Byte;
5365 begin
5366 assert(st <> nil);
5368 // Сигнатура игрока
5369 if not utils.checkSign(st, 'PLYR') then raise XStreamError.Create('invalid player signature');
5370 if (utils.readByte(st) <> PLR_SAVE_VERSION) then raise XStreamError.Create('invalid player version');
5371 // Бот или человек:
5372 FIamBot := utils.readBool(st);
5373 // UID игрока
5374 FUID := utils.readWord(st);
5375 // Имя игрока
5376 str := utils.readStr(st);
5377 if (self <> gPlayer1) and (self <> gPlayer2) then FName := str;
5378 // Команда
5379 FTeam := utils.readByte(st);
5380 // Жив ли
5381 FAlive := utils.readBool(st);
5382 // Израсходовал ли все жизни
5383 FNoRespawn := utils.readBool(st);
5384 // Направление
5385 b := utils.readByte(st);
5386 if b = 1 then FDirection := TDirection.D_LEFT else FDirection := TDirection.D_RIGHT; // b = 2
5387 // Здоровье
5388 FHealth := utils.readLongInt(st);
5389 // Коэффициент инвалидности
5390 FHandicap := utils.readLongInt(st);
5391 // Жизни
5392 FLives := utils.readByte(st);
5393 // Броня
5394 FArmor := utils.readLongInt(st);
5395 // Запас воздуха
5396 FAir := utils.readLongInt(st);
5397 // Запас горючего
5398 FJetFuel := utils.readLongInt(st);
5399 // Боль
5400 FPain := utils.readLongInt(st);
5401 // Убил
5402 FKills := utils.readLongInt(st);
5403 // Убил монстров
5404 FMonsterKills := utils.readLongInt(st);
5405 // Фрагов
5406 FFrags := utils.readLongInt(st);
5407 // Фрагов подряд
5408 FFragCombo := utils.readByte(st);
5409 // Время последнего фрага
5410 FLastFrag := utils.readLongWord(st);
5411 // Смертей
5412 FDeath := utils.readLongInt(st);
5413 // Какой флаг несет
5414 FFlag := utils.readByte(st);
5415 // Нашел секретов
5416 FSecrets := utils.readLongInt(st);
5417 // Текущее оружие
5418 FCurrWeap := utils.readByte(st);
5419 // Желаемое оружие
5420 FNextWeap := utils.readWord(st);
5421 // ...и пауза
5422 FNextWeapDelay := utils.readByte(st);
5423 // Время зарядки BFG
5424 FBFGFireCounter := utils.readSmallInt(st);
5425 // Буфер урона
5426 FDamageBuffer := utils.readLongInt(st);
5427 // Последний ударивший
5428 FLastSpawnerUID := utils.readWord(st);
5429 // Тип последнего полученного урона
5430 FLastHit := utils.readByte(st);
5431 // Объект игрока
5432 Obj_LoadState(@FObj, st);
5433 // Текущее количество патронов
5434 for i := A_BULLETS to A_HIGH do FAmmo[i] := utils.readWord(st);
5435 // Максимальное количество патронов
5436 for i := A_BULLETS to A_HIGH do FMaxAmmo[i] := utils.readWord(st);
5437 // Наличие оружия
5438 for i := WP_FIRST to WP_LAST do FWeapon[i] := utils.readBool(st);
5439 // Время перезарядки оружия
5440 for i := WP_FIRST to WP_LAST do FReloading[i] := utils.readWord(st);
5441 // Наличие рюкзака
5442 if utils.readBool(st) then Include(FRulez, R_ITEM_BACKPACK);
5443 // Наличие красного ключа
5444 if utils.readBool(st) then Include(FRulez, R_KEY_RED);
5445 // Наличие зеленого ключа
5446 if utils.readBool(st) then Include(FRulez, R_KEY_GREEN);
5447 // Наличие синего ключа
5448 if utils.readBool(st) then Include(FRulez, R_KEY_BLUE);
5449 // Наличие берсерка
5450 if utils.readBool(st) then Include(FRulez, R_BERSERK);
5451 // Время действия специальных предметов
5452 for i := MR_SUIT to MR_MAX do FMegaRulez[i] := utils.readLongWord(st);
5453 // Время до повторного респауна, смены оружия, исользования, захвата флага
5454 for i := T_RESPAWN to T_FLAGCAP do FTime[i] := utils.readLongWord(st);
5455 // Название модели
5456 str := utils.readStr(st);
5457 // Цвет модели
5458 FColor.R := utils.readByte(st);
5459 FColor.G := utils.readByte(st);
5460 FColor.B := utils.readByte(st);
5461 if (self = gPlayer1) then
5462 begin
5463 str := gPlayer1Settings.Model;
5464 FColor := gPlayer1Settings.Color;
5465 end
5466 else if (self = gPlayer2) then
5467 begin
5468 str := gPlayer2Settings.Model;
5469 FColor := gPlayer2Settings.Color;
5470 end;
5471 // Обновляем модель игрока
5472 SetModel(str);
5473 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
5474 FModel.Color := TEAMCOLOR[FTeam]
5475 else
5476 FModel.Color := FColor;
5477 end;
5480 procedure TPlayer.AllRulez(Health: Boolean);
5481 var
5482 a: Integer;
5483 begin
5484 if Health then
5485 begin
5486 FHealth := PLAYER_HP_LIMIT;
5487 FArmor := PLAYER_AP_LIMIT;
5488 Exit;
5489 end;
5491 for a := WP_FIRST to WP_LAST do FWeapon[a] := True;
5492 for a := A_BULLETS to A_HIGH do FAmmo[a] := 30000;
5493 FRulez := FRulez+[R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE];
5494 end;
5496 procedure TPlayer.RestoreHealthArmor();
5497 begin
5498 FHealth := PLAYER_HP_LIMIT;
5499 FArmor := PLAYER_AP_LIMIT;
5500 end;
5502 procedure TPlayer.FragCombo();
5503 var
5504 Param: Integer;
5505 begin
5506 if (gGameSettings.GameMode in [GM_COOP, GM_SINGLE]) or g_Game_IsClient then
5507 Exit;
5508 if gTime - FLastFrag < FRAG_COMBO_TIME then
5509 begin
5510 if FFragCombo < 5 then
5511 Inc(FFragCombo);
5512 Param := FUID or (FFragCombo shl 16);
5513 if (FComboEvnt >= Low(gDelayedEvents)) and
5514 (FComboEvnt <= High(gDelayedEvents)) and
5515 gDelayedEvents[FComboEvnt].Pending and
5516 (gDelayedEvents[FComboEvnt].DEType = DE_KILLCOMBO) and
5517 (gDelayedEvents[FComboEvnt].DENum and $FFFF = FUID) then
5518 begin
5519 gDelayedEvents[FComboEvnt].Time := gTime + 500;
5520 gDelayedEvents[FComboEvnt].DENum := Param;
5521 end
5522 else
5523 FComboEvnt := g_Game_DelayEvent(DE_KILLCOMBO, 500, Param);
5524 end
5525 else
5526 FFragCombo := 1;
5528 FLastFrag := gTime;
5529 end;
5531 procedure TPlayer.GiveItem(ItemType: Byte);
5532 begin
5533 case ItemType of
5534 ITEM_SUIT:
5535 if FMegaRulez[MR_SUIT] < gTime+PLAYER_SUIT_TIME then
5536 begin
5537 FMegaRulez[MR_SUIT] := gTime+PLAYER_SUIT_TIME;
5538 end;
5540 ITEM_OXYGEN:
5541 if FAir < AIR_MAX then
5542 begin
5543 FAir := AIR_MAX;
5544 end;
5546 ITEM_MEDKIT_BLACK:
5547 begin
5548 if not (R_BERSERK in FRulez) then
5549 begin
5550 Include(FRulez, R_BERSERK);
5551 if FBFGFireCounter < 1 then
5552 begin
5553 FCurrWeap := WEAPON_KASTET;
5554 resetWeaponQueue();
5555 FModel.SetWeapon(WEAPON_KASTET);
5556 end;
5557 if gFlash <> 0 then
5558 Inc(FPain, 100);
5559 FBerserk := gTime+30000;
5560 end;
5561 if FHealth < PLAYER_HP_SOFT then
5562 begin
5563 FHealth := PLAYER_HP_SOFT;
5564 FBerserk := gTime+30000;
5565 end;
5566 end;
5568 ITEM_INVUL:
5569 if FMegaRulez[MR_INVUL] < gTime+PLAYER_INVUL_TIME then
5570 begin
5571 FMegaRulez[MR_INVUL] := gTime+PLAYER_INVUL_TIME;
5572 FSpawnInvul := 0;
5573 end;
5575 ITEM_INVIS:
5576 if FMegaRulez[MR_INVIS] < gTime+PLAYER_INVIS_TIME then
5577 begin
5578 FMegaRulez[MR_INVIS] := gTime+PLAYER_INVIS_TIME;
5579 end;
5581 ITEM_JETPACK:
5582 if FJetFuel < JET_MAX then
5583 begin
5584 FJetFuel := JET_MAX;
5585 end;
5587 ITEM_MEDKIT_SMALL: if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 10, PLAYER_HP_SOFT);
5588 ITEM_MEDKIT_LARGE: if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 25, PLAYER_HP_SOFT);
5590 ITEM_ARMOR_GREEN: if FArmor < PLAYER_AP_SOFT then FArmor := PLAYER_AP_SOFT;
5591 ITEM_ARMOR_BLUE: if FArmor < PLAYER_AP_LIMIT then FArmor := PLAYER_AP_LIMIT;
5593 ITEM_SPHERE_BLUE: if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 100, PLAYER_HP_LIMIT);
5594 ITEM_SPHERE_WHITE:
5595 if (FHealth < PLAYER_HP_LIMIT) or (FArmor < PLAYER_AP_LIMIT) then
5596 begin
5597 if FHealth < PLAYER_HP_LIMIT then FHealth := PLAYER_HP_LIMIT;
5598 if FArmor < PLAYER_AP_LIMIT then FArmor := PLAYER_AP_LIMIT;
5599 end;
5601 ITEM_WEAPON_SAW: FWeapon[WEAPON_SAW] := True;
5602 ITEM_WEAPON_SHOTGUN1: FWeapon[WEAPON_SHOTGUN1] := True;
5603 ITEM_WEAPON_SHOTGUN2: FWeapon[WEAPON_SHOTGUN2] := True;
5604 ITEM_WEAPON_CHAINGUN: FWeapon[WEAPON_CHAINGUN] := True;
5605 ITEM_WEAPON_ROCKETLAUNCHER: FWeapon[WEAPON_ROCKETLAUNCHER] := True;
5606 ITEM_WEAPON_PLASMA: FWeapon[WEAPON_PLASMA] := True;
5607 ITEM_WEAPON_BFG: FWeapon[WEAPON_BFG] := True;
5608 ITEM_WEAPON_SUPERPULEMET: FWeapon[WEAPON_SUPERPULEMET] := True;
5609 ITEM_WEAPON_FLAMETHROWER: FWeapon[WEAPON_FLAMETHROWER] := True;
5611 ITEM_AMMO_BULLETS: if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
5612 ITEM_AMMO_BULLETS_BOX: if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then IncMax(FAmmo[A_BULLETS], 50, FMaxAmmo[A_BULLETS]);
5613 ITEM_AMMO_SHELLS: if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
5614 ITEM_AMMO_SHELLS_BOX: if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then IncMax(FAmmo[A_SHELLS], 25, FMaxAmmo[A_SHELLS]);
5615 ITEM_AMMO_ROCKET: if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
5616 ITEM_AMMO_ROCKET_BOX: if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then IncMax(FAmmo[A_ROCKETS], 5, FMaxAmmo[A_ROCKETS]);
5617 ITEM_AMMO_CELL: if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
5618 ITEM_AMMO_CELL_BIG: if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then IncMax(FAmmo[A_CELLS], 100, FMaxAmmo[A_CELLS]);
5619 ITEM_AMMO_FUELCAN: if FAmmo[A_FUEL] < FMaxAmmo[A_FUEL] then IncMax(FAmmo[A_FUEL], 100, FMaxAmmo[A_FUEL]);
5621 ITEM_AMMO_BACKPACK:
5622 if (FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS]) or
5623 (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or
5624 (FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS]) or
5625 (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or
5626 (FMaxAmmo[A_FUEL] < AmmoLimits[1, A_FUEL]) then
5627 begin
5628 FMaxAmmo[A_BULLETS] := AmmoLimits[1, A_BULLETS];
5629 FMaxAmmo[A_SHELLS] := AmmoLimits[1, A_SHELLS];
5630 FMaxAmmo[A_ROCKETS] := AmmoLimits[1, A_ROCKETS];
5631 FMaxAmmo[A_CELLS] := AmmoLimits[1, A_CELLS];
5632 FMaxAmmo[A_FUEL] := AmmoLimits[1, A_FUEL];
5634 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
5635 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
5636 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
5637 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
5639 FRulez := FRulez + [R_ITEM_BACKPACK];
5640 end;
5642 ITEM_KEY_RED: if not (R_KEY_RED in FRulez) then Include(FRulez, R_KEY_RED);
5643 ITEM_KEY_GREEN: if not (R_KEY_GREEN in FRulez) then Include(FRulez, R_KEY_GREEN);
5644 ITEM_KEY_BLUE: if not (R_KEY_BLUE in FRulez) then Include(FRulez, R_KEY_BLUE);
5646 ITEM_BOTTLE: if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 4, PLAYER_HP_LIMIT);
5647 ITEM_HELMET: if FArmor < PLAYER_AP_LIMIT then IncMax(FArmor, 5, PLAYER_AP_LIMIT);
5649 else
5650 Exit;
5651 end;
5652 if g_Game_IsNet and g_Game_IsServer then
5653 MH_SEND_PlayerStats(FUID);
5654 end;
5656 procedure TPlayer.FlySmoke(Times: DWORD = 1);
5657 var i: DWORD;
5658 begin
5659 if (Random(5) = 1) and (Times = 1) then
5660 Exit;
5662 if BodyInLiquid(0, 0) then
5663 begin
5664 g_GFX_Bubbles(Obj.X+Obj.Rect.X+(Obj.Rect.Width div 2)+Random(3)-1,
5665 Obj.Y+Obj.Rect.Height+8, 1, 8, 4);
5666 if Random(2) = 0 then
5667 g_Sound_PlayExAt('SOUND_GAME_BUBBLE1', FObj.X, FObj.Y)
5668 else
5669 g_Sound_PlayExAt('SOUND_GAME_BUBBLE2', FObj.X, FObj.Y);
5670 Exit;
5671 end;
5673 for i := 1 to Times do
5674 begin
5675 g_GFX_QueueEffect(
5676 R_GFX_SMOKE_TRANS,
5677 Obj.X+Obj.Rect.X+Random(Obj.Rect.Width+Times*2)-(R_GFX_SMOKE_WIDTH div 2),
5678 Obj.Y+Obj.Rect.Height-4+Random(8+Times*2)
5679 );
5680 end;
5681 end;
5683 procedure TPlayer.OnFireFlame(Times: DWORD = 1);
5684 var i: DWORD;
5685 begin
5686 if (Random(10) = 1) and (Times = 1) then
5687 Exit;
5689 for i := 1 to Times do
5690 begin
5691 g_GFX_QueueEffect(
5692 R_GFX_FLAME,
5693 Obj.X+Obj.Rect.X+Random(Obj.Rect.Width+Times*2)-(R_GFX_FLAME_WIDTH div 2),
5694 Obj.Y+8+Random(8+Times*2)
5695 );
5696 end;
5697 end;
5699 procedure TPlayer.PauseSounds(Enable: Boolean);
5700 begin
5701 FSawSound.Pause(Enable);
5702 FSawSoundIdle.Pause(Enable);
5703 FSawSoundHit.Pause(Enable);
5704 FSawSoundSelect.Pause(Enable);
5705 FFlameSoundOn.Pause(Enable);
5706 FFlameSoundOff.Pause(Enable);
5707 FFlameSoundWork.Pause(Enable);
5708 FJetSoundFly.Pause(Enable);
5709 FJetSoundOn.Pause(Enable);
5710 FJetSoundOff.Pause(Enable);
5711 end;
5713 { T C o r p s e : }
5715 constructor TCorpse.Create(X, Y: Integer; ModelName: String; aMess: Boolean);
5716 begin
5717 g_Obj_Init(@FObj);
5718 FObj.X := X;
5719 FObj.Y := Y;
5720 FObj.Rect := PLAYER_CORPSERECT;
5721 FMess := aMess;
5722 FModel := g_PlayerModel_Get(ModelName);
5724 if FMess then
5725 begin
5726 FState := CORPSE_STATE_MESS;
5727 FModel.ChangeAnimation(A_DIE2);
5728 end
5729 else
5730 begin
5731 FState := CORPSE_STATE_NORMAL;
5732 FModel.ChangeAnimation(A_DIE1);
5733 end;
5734 end;
5736 destructor TCorpse.Destroy();
5737 begin
5738 FModel.Free;
5739 inherited;
5740 end;
5742 function TCorpse.ObjPtr (): PObj; inline; begin result := @FObj; end;
5744 procedure TCorpse.positionChanged (); inline; begin end;
5746 procedure TCorpse.moveBy (dx, dy: Integer); inline;
5747 begin
5748 if (dx <> 0) or (dy <> 0) then
5749 begin
5750 FObj.X += dx;
5751 FObj.Y += dy;
5752 positionChanged();
5753 end;
5754 end;
5757 procedure TCorpse.getMapBox (out x, y, w, h: Integer); inline;
5758 begin
5759 x := FObj.X+PLAYER_CORPSERECT.X;
5760 y := FObj.Y+PLAYER_CORPSERECT.Y;
5761 w := PLAYER_CORPSERECT.Width;
5762 h := PLAYER_CORPSERECT.Height;
5763 end;
5766 procedure TCorpse.Damage(Value: Word; SpawnerUID: Word; vx, vy: Integer);
5767 var Blood: TModelBlood;
5768 begin
5769 if FState = CORPSE_STATE_REMOVEME then
5770 Exit;
5772 FDamage := FDamage + Value;
5774 if FDamage > 150 then
5775 begin
5776 if FModel <> nil then
5777 begin
5778 FState := CORPSE_STATE_REMOVEME;
5780 g_Player_CreateGibs(
5781 FObj.X + FObj.Rect.X + (FObj.Rect.Width div 2),
5782 FObj.Y + FObj.Rect.Y + (FObj.Rect.Height div 2),
5783 FModel.id,
5784 FModel.Color
5785 );
5787 // Звук мяса от трупа:
5788 FModel.PlaySound(MODELSOUND_DIE, 5, FObj.X, FObj.Y);
5790 // Зловещий смех:
5791 if (gBodyKillEvent <> -1) and gDelayedEvents[gBodyKillEvent].Pending then
5792 gDelayedEvents[gBodyKillEvent].Pending := False;
5793 gBodyKillEvent := g_Game_DelayEvent(DE_BODYKILL, 1050, SpawnerUID);
5795 FModel.Free;
5796 FModel := nil;
5797 end
5798 end
5799 else
5800 begin
5801 Blood := FModel.GetBlood();
5802 FObj.Vel.X := FObj.Vel.X + vx;
5803 FObj.Vel.Y := FObj.Vel.Y + vy;
5804 g_GFX_Blood(FObj.X+PLAYER_CORPSERECT.X+(PLAYER_CORPSERECT.Width div 2),
5805 FObj.Y+PLAYER_CORPSERECT.Y+(PLAYER_CORPSERECT.Height div 2),
5806 Value, vx, vy, 16, (PLAYER_CORPSERECT.Height*2) div 3,
5807 Blood.R, Blood.G, Blood.B, Blood.Kind);
5808 end;
5809 end;
5811 procedure TCorpse.Update();
5812 var
5813 st: Word;
5814 begin
5815 if FState = CORPSE_STATE_REMOVEME then
5816 Exit;
5818 FObj.oldX := FObj.X;
5819 FObj.oldY := FObj.Y;
5821 if gTime mod (GAME_TICK*2) <> 0 then
5822 begin
5823 g_Obj_Move(@FObj, True, True, True);
5824 positionChanged(); // this updates spatial accelerators
5825 Exit;
5826 end;
5828 // Сопротивление воздуха для трупа:
5829 FObj.Vel.X := z_dec(FObj.Vel.X, 1);
5831 st := g_Obj_Move(@FObj, True, True, True);
5832 positionChanged(); // this updates spatial accelerators
5834 if WordBool(st and MOVE_FALLOUT) then
5835 begin
5836 FState := CORPSE_STATE_REMOVEME;
5837 Exit;
5838 end;
5840 if FModel <> nil then
5841 FModel.Update;
5842 end;
5845 procedure TCorpse.SaveState (st: TStream);
5846 var anim: Boolean;
5847 begin
5848 assert(st <> nil);
5850 // Сигнатура трупа
5851 utils.writeSign(st, 'CORP');
5852 utils.writeInt(st, Byte(0));
5853 // Состояние
5854 utils.writeInt(st, Byte(FState));
5855 // Накопленный урон
5856 utils.writeInt(st, Byte(FDamage));
5857 // Цвет
5858 utils.writeInt(st, Byte(FModel.Color.R));
5859 utils.writeInt(st, Byte(FModel.Color.G));
5860 utils.writeInt(st, Byte(FModel.Color.B));
5861 // Объект трупа
5862 Obj_SaveState(st, @FObj);
5863 utils.writeInt(st, Word(FPlayerUID));
5864 // animation
5865 anim := (FModel <> nil);
5866 utils.writeBool(st, anim);
5867 if anim then FModel.AnimState.SaveState(st);
5868 // animation for mask (same as animation, compat with older saves)
5869 anim := (FModel <> nil);
5870 utils.writeBool(st, anim);
5871 if anim then FModel.AnimState.SaveState(st);
5872 end;
5875 procedure TCorpse.LoadState (st: TStream);
5876 var anim: Boolean; r, g, b: Byte; stub: TAnimationState;
5877 begin
5878 assert(st <> nil);
5880 // Сигнатура трупа
5881 if not utils.checkSign(st, 'CORP') then raise XStreamError.Create('invalid corpse signature');
5882 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid corpse version');
5883 // Состояние
5884 FState := utils.readByte(st);
5885 // Накопленный урон
5886 FDamage := utils.readByte(st);
5887 // Цвет
5888 r := utils.readByte(st);
5889 g := utils.readByte(st);
5890 b := utils.readByte(st);
5891 FModel.SetColor(r, g, b);
5892 // Объект трупа
5893 Obj_LoadState(@FObj, st);
5894 FPlayerUID := utils.readWord(st);
5895 // animation
5896 stub := TAnimationState.Create(False, 0, 0);
5897 anim := utils.readBool(st);
5898 if anim then
5899 begin
5900 stub.LoadState(st);
5901 FModel.AnimState.CurrentFrame := Min(stub.CurrentFrame, FModel.AnimState.Length);
5902 end
5903 else
5904 begin
5905 FModel.Free;
5906 FModel := nil
5907 end;
5908 // animation for mask (same as animation, compat with older saves)
5909 anim := utils.readBool(st);
5910 if anim then stub.LoadState(st);
5911 stub.Free;
5912 end;
5914 { T B o t : }
5916 constructor TBot.Create();
5917 var
5918 a: Integer;
5919 begin
5920 inherited Create();
5922 FPhysics := True;
5923 FSpectator := False;
5924 FGhost := False;
5926 FIamBot := True;
5928 Inc(gNumBots);
5930 for a := WP_FIRST to WP_LAST do
5931 begin
5932 FDifficult.WeaponPrior[a] := WEAPON_PRIOR1[a];
5933 FDifficult.CloseWeaponPrior[a] := WEAPON_PRIOR2[a];
5934 //FDifficult.SafeWeaponPrior[a] := WEAPON_PRIOR3[a];
5935 end;
5936 end;
5938 destructor TBot.Destroy();
5939 begin
5940 Dec(gNumBots);
5941 inherited Destroy();
5942 end;
5944 procedure TBot.Respawn(Silent: Boolean; Force: Boolean = False);
5945 begin
5946 inherited Respawn(Silent, Force);
5948 FAIFlags := nil;
5949 FSelectedWeapon := FCurrWeap;
5950 resetWeaponQueue();
5951 FTargetUID := 0;
5952 end;
5954 procedure TBot.UpdateCombat();
5955 type
5956 TTarget = record
5957 UID: Word;
5958 X, Y: Integer;
5959 Rect: TRectWH;
5960 cX, cY: Integer;
5961 Dist: Word;
5962 Line: Boolean;
5963 Visible: Boolean;
5964 IsPlayer: Boolean;
5965 end;
5967 TTargetRecord = array of TTarget;
5969 function Compare(a, b: TTarget): Integer;
5970 begin
5971 if a.Line and not b.Line then // A на линии огня
5972 Result := -1
5973 else
5974 if not a.Line and b.Line then // B на линии огня
5975 Result := 1
5976 else // И A, и B на линии или не на линии огня
5977 if (a.Line and b.Line) or ((not a.Line) and (not b.Line)) then
5978 begin
5979 if a.Dist > b.Dist then // B ближе
5980 Result := 1
5981 else // A ближе или равноудаленно с B
5982 Result := -1;
5983 end
5984 else // Странно -> A
5985 Result := -1;
5986 end;
5988 var
5989 a, x1, y1, x2, y2: Integer;
5990 targets: TTargetRecord;
5991 ammo: Word;
5992 Target, BestTarget: TTarget;
5993 firew, fireh: Integer;
5994 angle: SmallInt;
5995 mon: TMonster;
5996 pla, tpla: TPlayer;
5997 vsPlayer, vsMonster, ok: Boolean;
6000 function monsUpdate (mon: TMonster): Boolean;
6001 begin
6002 result := false; // don't stop
6003 if mon.alive and (mon.MonsterType <> MONSTER_BARREL) then
6004 begin
6005 if not TargetOnScreen(mon.Obj.X+mon.Obj.Rect.X, mon.Obj.Y+mon.Obj.Rect.Y) then exit;
6007 x2 := mon.Obj.X+mon.Obj.Rect.X+(mon.Obj.Rect.Width div 2);
6008 y2 := mon.Obj.Y+mon.Obj.Rect.Y+(mon.Obj.Rect.Height div 2);
6010 // Если монстр на экране и не прикрыт стеной
6011 if g_TraceVector(x1, y1, x2, y2) then
6012 begin
6013 // Добавляем к списку возможных целей
6014 SetLength(targets, Length(targets)+1);
6015 with targets[High(targets)] do
6016 begin
6017 UID := mon.UID;
6018 X := mon.Obj.X;
6019 Y := mon.Obj.Y;
6020 cX := x2;
6021 cY := y2;
6022 Rect := mon.Obj.Rect;
6023 Dist := g_PatchLength(x1, y1, x2, y2);
6024 Line := (y1+4 < Target.Y + mon.Obj.Rect.Y + mon.Obj.Rect.Height) and
6025 (y1-4 > Target.Y + mon.Obj.Rect.Y);
6026 Visible := True;
6027 IsPlayer := False;
6028 end;
6029 end;
6030 end;
6031 end;
6033 begin
6034 vsPlayer := LongBool(gGameSettings.Options and GAME_OPTION_BOTVSPLAYER);
6035 vsMonster := LongBool(gGameSettings.Options and GAME_OPTION_BOTVSMONSTER);
6037 // Если текущее оружие не то, что нужно, то меняем:
6038 if FCurrWeap <> FSelectedWeapon then
6039 NextWeapon();
6041 // Если нужно стрелять и нужное оружие, то нажать "Стрелять":
6042 if (GetAIFlag('NEEDFIRE') <> '') and (FCurrWeap = FSelectedWeapon) then
6043 begin
6044 RemoveAIFlag('NEEDFIRE');
6046 case FCurrWeap of
6047 WEAPON_PLASMA, WEAPON_SUPERPULEMET, WEAPON_CHAINGUN: PressKey(KEY_FIRE, 20);
6048 WEAPON_SAW, WEAPON_KASTET, WEAPON_FLAMETHROWER: PressKey(KEY_FIRE, 40);
6049 else PressKey(KEY_FIRE);
6050 end;
6051 end;
6053 // Координаты ствола:
6054 x1 := FObj.X + WEAPONPOINT[FDirection].X;
6055 y1 := FObj.Y + WEAPONPOINT[FDirection].Y;
6057 Target.UID := FTargetUID;
6059 ok := False;
6060 if Target.UID <> 0 then
6061 begin // Цель есть - настраиваем
6062 if (g_GetUIDType(Target.UID) = UID_PLAYER) and
6063 vsPlayer then
6064 begin // Игрок
6065 tpla := g_Player_Get(Target.UID);
6066 if tpla <> nil then
6067 with tpla do
6068 begin
6069 if (@FObj) <> nil then
6070 begin
6071 Target.X := FObj.X;
6072 Target.Y := FObj.Y;
6073 end;
6074 end;
6076 Target.cX := Target.X + PLAYER_RECT_CX;
6077 Target.cY := Target.Y + PLAYER_RECT_CY;
6078 Target.Rect := PLAYER_RECT;
6079 Target.Visible := g_TraceVector(x1, y1, Target.cX, Target.cY);
6080 Target.Line := (y1+4 < Target.Y+PLAYER_RECT.Y+PLAYER_RECT.Height) and
6081 (y1-4 > Target.Y+PLAYER_RECT.Y);
6082 Target.IsPlayer := True;
6083 ok := True;
6084 end
6085 else
6086 if (g_GetUIDType(Target.UID) = UID_MONSTER) and
6087 vsMonster then
6088 begin // Монстр
6089 mon := g_Monsters_ByUID(Target.UID);
6090 if mon <> nil then
6091 begin
6092 Target.X := mon.Obj.X;
6093 Target.Y := mon.Obj.Y;
6095 Target.cX := Target.X + mon.Obj.Rect.X + (mon.Obj.Rect.Width div 2);
6096 Target.cY := Target.Y + mon.Obj.Rect.Y + (mon.Obj.Rect.Height div 2);
6097 Target.Rect := mon.Obj.Rect;
6098 Target.Visible := g_TraceVector(x1, y1, Target.cX, Target.cY);
6099 Target.Line := (y1+4 < Target.Y + mon.Obj.Rect.Y + mon.Obj.Rect.Height) and
6100 (y1-4 > Target.Y + mon.Obj.Rect.Y);
6101 Target.IsPlayer := False;
6102 ok := True;
6103 end;
6104 end;
6105 end;
6107 if not ok then
6108 begin // Цели нет - обнуляем
6109 Target.X := 0;
6110 Target.Y := 0;
6111 Target.cX := 0;
6112 Target.cY := 0;
6113 Target.Visible := False;
6114 Target.Line := False;
6115 Target.IsPlayer := False;
6116 end;
6118 targets := nil;
6120 // Если цель не видима или не на линии огня, то ищем все возможные цели:
6121 if (not Target.Line) or (not Target.Visible) then
6122 begin
6123 // Игроки:
6124 if vsPlayer then
6125 for a := 0 to High(gPlayers) do
6126 if (gPlayers[a] <> nil) and (gPlayers[a].alive) and
6127 (gPlayers[a].FUID <> FUID) and
6128 (not SameTeam(FUID, gPlayers[a].FUID)) and
6129 (not gPlayers[a].NoTarget) and
6130 (gPlayers[a].FMegaRulez[MR_INVIS] < gTime) then
6131 begin
6132 if not TargetOnScreen(gPlayers[a].FObj.X + PLAYER_RECT.X,
6133 gPlayers[a].FObj.Y + PLAYER_RECT.Y) then
6134 Continue;
6136 x2 := gPlayers[a].FObj.X + PLAYER_RECT_CX;
6137 y2 := gPlayers[a].FObj.Y + PLAYER_RECT_CY;
6139 // Если игрок на экране и не прикрыт стеной:
6140 if g_TraceVector(x1, y1, x2, y2) then
6141 begin
6142 // Добавляем к списку возможных целей:
6143 SetLength(targets, Length(targets)+1);
6144 with targets[High(targets)] do
6145 begin
6146 UID := gPlayers[a].FUID;
6147 X := gPlayers[a].FObj.X;
6148 Y := gPlayers[a].FObj.Y;
6149 cX := x2;
6150 cY := y2;
6151 Rect := PLAYER_RECT;
6152 Dist := g_PatchLength(x1, y1, x2, y2);
6153 Line := (y1+4 < Target.Y+PLAYER_RECT.Y+PLAYER_RECT.Height) and
6154 (y1-4 > Target.Y+PLAYER_RECT.Y);
6155 Visible := True;
6156 IsPlayer := True;
6157 end;
6158 end;
6159 end;
6161 // Монстры:
6162 if vsMonster then g_Mons_ForEach(monsUpdate);
6163 end;
6165 // Если есть возможные цели:
6166 // (Выбираем лучшую, меняем оружие и бежим к ней/от нее)
6167 if targets <> nil then
6168 begin
6169 // Выбираем наилучшую цель:
6170 BestTarget := targets[0];
6171 if Length(targets) > 1 then
6172 for a := 1 to High(targets) do
6173 if Compare(BestTarget, targets[a]) = 1 then
6174 BestTarget := targets[a];
6176 // Если лучшая цель "виднее" текущей, то текущая := лучшая:
6177 if ((not Target.Visible) and BestTarget.Visible and (Target.UID <> BestTarget.UID)) or
6178 ((not Target.Line) and BestTarget.Line and BestTarget.Visible) then
6179 begin
6180 Target := BestTarget;
6182 if (Healthy() = 3) or ((Healthy() = 2)) then
6183 begin // Если здоровы - догоняем
6184 if ((RunDirection() = TDirection.D_LEFT) and (Target.X > FObj.X)) then
6185 SetAIFlag('GORIGHT', '1');
6186 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X < FObj.X)) then
6187 SetAIFlag('GOLEFT', '1');
6188 end
6189 else
6190 begin // Если побиты - убегаем
6191 if ((RunDirection() = TDirection.D_LEFT) and (Target.X < FObj.X)) then
6192 SetAIFlag('GORIGHT', '1');
6193 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X > FObj.X)) then
6194 SetAIFlag('GOLEFT', '1');
6195 end;
6197 // Выбираем оружие на основе расстояния и приоритетов:
6198 SelectWeapon(Abs(x1-Target.cX));
6199 end;
6200 end;
6202 // Если есть цель:
6203 // (Догоняем/убегаем, стреляем по направлению к цели)
6204 // (Если цель далеко, то хватит следить за ней)
6205 if Target.UID <> 0 then
6206 begin
6207 if not TargetOnScreen(Target.X + Target.Rect.X,
6208 Target.Y + Target.Rect.Y) then
6209 begin // Цель сбежала с "экрана"
6210 if (Healthy() = 3) or ((Healthy() = 2)) then
6211 begin // Если здоровы - догоняем
6212 if ((RunDirection() = TDirection.D_LEFT) and (Target.X > FObj.X)) then
6213 SetAIFlag('GORIGHT', '1');
6214 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X < FObj.X)) then
6215 SetAIFlag('GOLEFT', '1');
6216 end
6217 else
6218 begin // Если побиты - забываем о цели и убегаем
6219 Target.UID := 0;
6220 if ((RunDirection() = TDirection.D_LEFT) and (Target.X < FObj.X)) then
6221 SetAIFlag('GORIGHT', '1');
6222 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X > FObj.X)) then
6223 SetAIFlag('GOLEFT', '1');
6224 end;
6225 end
6226 else
6227 begin // Цель пока на "экране"
6228 // Если цель не загорожена стеной, то отмечаем, когда ее видели:
6229 if g_TraceVector(x1, y1, Target.cX, Target.cY) then
6230 FLastVisible := gTime;
6231 // Если разница высот не велика, то догоняем:
6232 if (Abs(FObj.Y-Target.Y) <= 128) then
6233 begin
6234 if ((RunDirection() = TDirection.D_LEFT) and (Target.X > FObj.X)) then
6235 SetAIFlag('GORIGHT', '1');
6236 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X < FObj.X)) then
6237 SetAIFlag('GOLEFT', '1');
6238 end;
6239 end;
6241 // Выбираем угол вверх:
6242 if FDirection = TDirection.D_LEFT then
6243 angle := ANGLE_LEFTUP
6244 else
6245 angle := ANGLE_RIGHTUP;
6247 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6248 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6250 // Если при угле вверх можно попасть в приблизительное положение цели:
6251 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6252 Target.X+Target.Rect.X+GetInterval(FDifficult.DiagPrecision, 128), //96
6253 Target.Y+Target.Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6254 Target.Rect.Width, Target.Rect.Height) and
6255 g_TraceVector(x1, y1, Target.cX, Target.cY) then
6256 begin // то нужно стрелять вверх
6257 SetAIFlag('NEEDFIRE', '1');
6258 SetAIFlag('NEEDSEEUP', '1');
6259 end;
6261 // Выбираем угол вниз:
6262 if FDirection = TDirection.D_LEFT then
6263 angle := ANGLE_LEFTDOWN
6264 else
6265 angle := ANGLE_RIGHTDOWN;
6267 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6268 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6270 // Если при угле вниз можно попасть в приблизительное положение цели:
6271 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6272 Target.X+Target.Rect.X+GetInterval(FDifficult.DiagPrecision, 128),
6273 Target.Y+Target.Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6274 Target.Rect.Width, Target.Rect.Height) and
6275 g_TraceVector(x1, y1, Target.cX, Target.cY) then
6276 begin // то нужно стрелять вниз
6277 SetAIFlag('NEEDFIRE', '1');
6278 SetAIFlag('NEEDSEEDOWN', '1');
6279 end;
6281 // Если цель видно и она на такой же высоте:
6282 if Target.Visible and
6283 (y1+4 < Target.Y+Target.Rect.Y+Target.Rect.Height) and
6284 (y1-4 > Target.Y+Target.Rect.Y) then
6285 begin
6286 // Если идем в сторону цели, то надо стрелять:
6287 if ((FDirection = TDirection.D_LEFT) and (Target.X < FObj.X)) or
6288 ((FDirection = TDirection.D_RIGHT) and (Target.X > FObj.X)) then
6289 begin // то нужно стрелять вперед
6290 SetAIFlag('NEEDFIRE', '1');
6291 SetAIFlag('NEEDSEEDOWN', '');
6292 SetAIFlag('NEEDSEEUP', '');
6293 end;
6294 // Если цель в пределах "экрана" и сложность позволяет прыжки сближения:
6295 if Abs(FObj.X-Target.X) < Trunc(gPlayerScreenSize.X*0.75) then
6296 if GetRnd(FDifficult.CloseJump) then
6297 begin // то если повезет - прыгаем (особенно, если близко)
6298 if Abs(FObj.X-Target.X) < 128 then
6299 a := 4
6300 else
6301 a := 30;
6302 if Random(a) = 0 then
6303 SetAIFlag('NEEDJUMP', '1');
6304 end;
6305 end;
6307 // Если цель все еще есть:
6308 if Target.UID <> 0 then
6309 if gTime-FLastVisible > 2000 then // Если видели давно
6310 Target.UID := 0 // то забыть цель
6311 else // Если видели недавно
6312 begin // но цель убили
6313 if Target.IsPlayer then
6314 begin // Цель - игрок
6315 pla := g_Player_Get(Target.UID);
6316 if (pla = nil) or (not pla.alive) or pla.NoTarget or
6317 (pla.FMegaRulez[MR_INVIS] >= gTime) then
6318 Target.UID := 0; // то забыть цель
6319 end
6320 else
6321 begin // Цель - монстр
6322 mon := g_Monsters_ByUID(Target.UID);
6323 if (mon = nil) or (not mon.alive) then
6324 Target.UID := 0; // то забыть цель
6325 end;
6326 end;
6327 end; // if Target.UID <> 0
6329 FTargetUID := Target.UID;
6331 // Если возможных целей нет:
6332 // (Атака чего-нибудь слева или справа)
6333 if targets = nil then
6334 if GetAIFlag('ATTACKLEFT') <> '' then
6335 begin // Если нужно атаковать налево
6336 RemoveAIFlag('ATTACKLEFT');
6338 SetAIFlag('NEEDJUMP', '1');
6340 if RunDirection() = TDirection.D_RIGHT then
6341 begin // Идем не в ту сторону
6342 if (Healthy() > 1) and GetRnd(FDifficult.InvisFire) then
6343 begin // Если здоровы, то, возможно, стреляем бежим влево и стреляем
6344 SetAIFlag('NEEDFIRE', '1');
6345 SetAIFlag('GOLEFT', '1');
6346 end;
6347 end
6348 else
6349 begin // Идем в нужную сторону
6350 if GetRnd(FDifficult.InvisFire) then // Возможно, стреляем вслепую
6351 SetAIFlag('NEEDFIRE', '1');
6352 if Healthy() <= 1 then // Побиты - убегаем
6353 SetAIFlag('GORIGHT', '1');
6354 end;
6355 end
6356 else
6357 if GetAIFlag('ATTACKRIGHT') <> '' then
6358 begin // Если нужно атаковать направо
6359 RemoveAIFlag('ATTACKRIGHT');
6361 SetAIFlag('NEEDJUMP', '1');
6363 if RunDirection() = TDirection.D_LEFT then
6364 begin // Идем не в ту сторону
6365 if (Healthy() > 1) and GetRnd(FDifficult.InvisFire) then
6366 begin // Если здоровы, то, возможно, бежим вправо и стреляем
6367 SetAIFlag('NEEDFIRE', '1');
6368 SetAIFlag('GORIGHT', '1');
6369 end;
6370 end
6371 else
6372 begin
6373 if GetRnd(FDifficult.InvisFire) then // Возможно, стреляем вслепую
6374 SetAIFlag('NEEDFIRE', '1');
6375 if Healthy() <= 1 then // Побиты - убегаем
6376 SetAIFlag('GOLEFT', '1');
6377 end;
6378 end;
6380 //HACK! (does it belongs there?)
6381 RealizeCurrentWeapon();
6383 // Если есть возможные цели:
6384 // (Стреляем по направлению к целям)
6385 if (targets <> nil) and (GetAIFlag('NEEDFIRE') <> '') then
6386 for a := 0 to High(targets) do
6387 begin
6388 // Если можем стрелять по диагонали:
6389 if GetRnd(FDifficult.DiagFire) then
6390 begin
6391 // Ищем цель сверху и стреляем, если есть:
6392 if FDirection = TDirection.D_LEFT then
6393 angle := ANGLE_LEFTUP
6394 else
6395 angle := ANGLE_RIGHTUP;
6397 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6398 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6400 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6401 targets[a].X+targets[a].Rect.X+GetInterval(FDifficult.DiagPrecision, 128),
6402 targets[a].Y+targets[a].Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6403 targets[a].Rect.Width, targets[a].Rect.Height) and
6404 g_TraceVector(x1, y1, targets[a].cX, targets[a].cY) then
6405 begin
6406 SetAIFlag('NEEDFIRE', '1');
6407 SetAIFlag('NEEDSEEUP', '1');
6408 end;
6410 // Ищем цель снизу и стреляем, если есть:
6411 if FDirection = TDirection.D_LEFT then
6412 angle := ANGLE_LEFTDOWN
6413 else
6414 angle := ANGLE_RIGHTDOWN;
6416 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6417 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6419 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6420 targets[a].X+targets[a].Rect.X+GetInterval(FDifficult.DiagPrecision, 128),
6421 targets[a].Y+targets[a].Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6422 targets[a].Rect.Width, targets[a].Rect.Height) and
6423 g_TraceVector(x1, y1, targets[a].cX, targets[a].cY) then
6424 begin
6425 SetAIFlag('NEEDFIRE', '1');
6426 SetAIFlag('NEEDSEEDOWN', '1');
6427 end;
6428 end;
6430 // Если цель "перед носом", то стреляем:
6431 if targets[a].Line and targets[a].Visible and
6432 (((FDirection = TDirection.D_LEFT) and (targets[a].X < FObj.X)) or
6433 ((FDirection = TDirection.D_RIGHT) and (targets[a].X > FObj.X))) then
6434 begin
6435 SetAIFlag('NEEDFIRE', '1');
6436 Break;
6437 end;
6438 end;
6440 // Если летит пуля, то, возможно, подпрыгиваем:
6441 if g_Weapon_Danger(FUID, FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y,
6442 PLAYER_RECT.Width, PLAYER_RECT.Height,
6443 40+GetInterval(FDifficult.Cover, 40)) then
6444 SetAIFlag('NEEDJUMP', '1');
6446 // Если кончились паторны, то нужно сменить оружие:
6447 ammo := GetAmmoByWeapon(FCurrWeap);
6448 if ((FCurrWeap = WEAPON_SHOTGUN2) and (ammo < 2)) or
6449 ((FCurrWeap = WEAPON_BFG) and (ammo < 40)) or
6450 (ammo = 0) then
6451 SetAIFlag('SELECTWEAPON', '1');
6453 // Если нужно сменить оружие, то выбираем нужное:
6454 if GetAIFlag('SELECTWEAPON') = '1' then
6455 begin
6456 SelectWeapon(-1);
6457 RemoveAIFlag('SELECTWEAPON');
6458 end;
6459 end;
6461 procedure TBot.Update();
6462 var
6463 EnableAI: Boolean;
6464 begin
6465 if not FAlive then
6466 begin // Respawn
6467 ReleaseKeys();
6468 PressKey(KEY_UP);
6469 end
6470 else
6471 begin
6472 EnableAI := True;
6474 // Проверяем, отключён ли AI ботов
6475 if (g_debug_BotAIOff = 1) and (Team = TEAM_RED) then
6476 EnableAI := False;
6477 if (g_debug_BotAIOff = 2) and (Team = TEAM_BLUE) then
6478 EnableAI := False;
6479 if g_debug_BotAIOff = 3 then
6480 EnableAI := False;
6482 if EnableAI then
6483 begin
6484 UpdateMove();
6485 UpdateCombat();
6486 end
6487 else
6488 begin
6489 RealizeCurrentWeapon();
6490 end;
6491 end;
6493 inherited Update();
6494 end;
6496 procedure TBot.ReleaseKey(Key: Byte);
6497 begin
6498 with FKeys[Key] do
6499 begin
6500 Pressed := False;
6501 Time := 0;
6502 end;
6503 end;
6505 function TBot.KeyPressed(Key: Word): Boolean;
6506 begin
6507 Result := FKeys[Key].Pressed;
6508 end;
6510 function TBot.GetAIFlag(aName: String20): String20;
6511 var
6512 a: Integer;
6513 begin
6514 Result := '';
6516 aName := LowerCase(aName);
6518 if FAIFlags <> nil then
6519 for a := 0 to High(FAIFlags) do
6520 if LowerCase(FAIFlags[a].Name) = aName then
6521 begin
6522 Result := FAIFlags[a].Value;
6523 Break;
6524 end;
6525 end;
6527 procedure TBot.RemoveAIFlag(aName: String20);
6528 var
6529 a, b: Integer;
6530 begin
6531 if FAIFlags = nil then Exit;
6533 aName := LowerCase(aName);
6535 for a := 0 to High(FAIFlags) do
6536 if LowerCase(FAIFlags[a].Name) = aName then
6537 begin
6538 if a <> High(FAIFlags) then
6539 for b := a to High(FAIFlags)-1 do
6540 FAIFlags[b] := FAIFlags[b+1];
6542 SetLength(FAIFlags, Length(FAIFlags)-1);
6543 Break;
6544 end;
6545 end;
6547 procedure TBot.SetAIFlag(aName, fValue: String20);
6548 var
6549 a: Integer;
6550 ok: Boolean;
6551 begin
6552 a := 0;
6553 ok := False;
6555 aName := LowerCase(aName);
6557 if FAIFlags <> nil then
6558 for a := 0 to High(FAIFlags) do
6559 if LowerCase(FAIFlags[a].Name) = aName then
6560 begin
6561 ok := True;
6562 Break;
6563 end;
6565 if ok then FAIFlags[a].Value := fValue
6566 else
6567 begin
6568 SetLength(FAIFlags, Length(FAIFlags)+1);
6569 with FAIFlags[High(FAIFlags)] do
6570 begin
6571 Name := aName;
6572 Value := fValue;
6573 end;
6574 end;
6575 end;
6577 procedure TBot.UpdateMove;
6579 procedure GoLeft(Time: Word = 1);
6580 begin
6581 ReleaseKey(KEY_LEFT);
6582 ReleaseKey(KEY_RIGHT);
6583 PressKey(KEY_LEFT, Time);
6584 SetDirection(TDirection.D_LEFT);
6585 end;
6587 procedure GoRight(Time: Word = 1);
6588 begin
6589 ReleaseKey(KEY_LEFT);
6590 ReleaseKey(KEY_RIGHT);
6591 PressKey(KEY_RIGHT, Time);
6592 SetDirection(TDirection.D_RIGHT);
6593 end;
6595 function Rnd(a: Word): Boolean;
6596 begin
6597 Result := Random(a) = 0;
6598 end;
6600 procedure Turn(Time: Word = 1200);
6601 begin
6602 if RunDirection() = TDirection.D_LEFT then GoRight(Time) else GoLeft(Time);
6603 end;
6605 procedure Stop();
6606 begin
6607 ReleaseKey(KEY_LEFT);
6608 ReleaseKey(KEY_RIGHT);
6609 end;
6611 function CanRunLeft(): Boolean;
6612 begin
6613 Result := not CollideLevel(-1, 0);
6614 end;
6616 function CanRunRight(): Boolean;
6617 begin
6618 Result := not CollideLevel(1, 0);
6619 end;
6621 function CanRun(): Boolean;
6622 begin
6623 if RunDirection() = TDirection.D_LEFT then Result := CanRunLeft() else Result := CanRunRight();
6624 end;
6626 procedure Jump(Time: Word = 30);
6627 begin
6628 PressKey(KEY_JUMP, Time);
6629 end;
6631 function NearHole(): Boolean;
6632 var
6633 x, sx: Integer;
6634 begin
6635 { TODO 5 : Лестницы }
6636 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6637 for x := 1 to PLAYER_RECT.Width do
6638 if (not StayOnStep(x*sx, 0)) and
6639 (not CollideLevel(x*sx, PLAYER_RECT.Height)) and
6640 (not CollideLevel(x*sx, PLAYER_RECT.Height*2)) then
6641 begin
6642 Result := True;
6643 Exit;
6644 end;
6646 Result := False;
6647 end;
6649 function BorderHole(): Boolean;
6650 var
6651 x, sx, xx: Integer;
6652 begin
6653 { TODO 5 : Лестницы }
6654 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6655 for x := 1 to PLAYER_RECT.Width do
6656 if (not StayOnStep(x*sx, 0)) and
6657 (not CollideLevel(x*sx, PLAYER_RECT.Height)) and
6658 (not CollideLevel(x*sx, PLAYER_RECT.Height*2)) then
6659 begin
6660 for xx := x to x+32 do
6661 if CollideLevel(xx*sx, PLAYER_RECT.Height) then
6662 begin
6663 Result := True;
6664 Exit;
6665 end;
6666 end;
6668 Result := False;
6669 end;
6671 function NearDeepHole(): Boolean;
6672 var
6673 x, sx, y: Integer;
6674 begin
6675 Result := False;
6677 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6678 y := 3;
6680 for x := 1 to PLAYER_RECT.Width do
6681 if (not StayOnStep(x*sx, 0)) and
6682 (not CollideLevel(x*sx, PLAYER_RECT.Height)) and
6683 (not CollideLevel(x*sx, PLAYER_RECT.Height*2)) then
6684 begin
6685 while FObj.Y+y*PLAYER_RECT.Height < gMapInfo.Height do
6686 begin
6687 if CollideLevel(x*sx, PLAYER_RECT.Height*y) then Exit;
6688 y := y+1;
6689 end;
6691 Result := True;
6692 end else Result := False;
6693 end;
6695 function OverDeepHole(): Boolean;
6696 var
6697 y: Integer;
6698 begin
6699 Result := False;
6701 y := 1;
6702 while FObj.Y+y*PLAYER_RECT.Height < gMapInfo.Height do
6703 begin
6704 if CollideLevel(0, PLAYER_RECT.Height*y) then Exit;
6705 y := y+1;
6706 end;
6708 Result := True;
6709 end;
6711 function OnGround(): Boolean;
6712 begin
6713 Result := StayOnStep(0, 0) or CollideLevel(0, 1);
6714 end;
6716 function OnLadder(): Boolean;
6717 begin
6718 Result := FullInStep(0, 0);
6719 end;
6721 function BelowLadder(): Boolean;
6722 begin
6723 Result := (FullInStep(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height) and
6724 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height)) or
6725 (FullInStep(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP) and
6726 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP));
6727 end;
6729 function BelowLiftUp(): Boolean;
6730 begin
6731 Result := ((FullInLift(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height) = -1) and
6732 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height)) or
6733 ((FullInLift(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP) = -1) and
6734 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP));
6735 end;
6737 function OnTopLift(): Boolean;
6738 begin
6739 Result := (FullInLift(0, 0) = -1) and (FullInLift(0, -32) = 0);
6740 end;
6742 function CanJumpOver(): Boolean;
6743 var
6744 sx, y: Integer;
6745 begin
6746 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6748 Result := False;
6750 if not CollideLevel(sx, 0) then Exit;
6752 for y := 1 to BOT_MAXJUMP do
6753 if CollideLevel(0, -y) then Exit else
6754 if not CollideLevel(sx, -y) then
6755 begin
6756 Result := True;
6757 Exit;
6758 end;
6759 end;
6761 function CanJumpUp(Dist: ShortInt): Boolean;
6762 var
6763 y, yy: Integer;
6764 c: Boolean;
6765 begin
6766 Result := False;
6768 if CollideLevel(Dist, 0) then Exit;
6770 c := False;
6771 for y := 0 to BOT_MAXJUMP do
6772 if CollideLevel(Dist, -y) then
6773 begin
6774 c := True;
6775 Break;
6776 end;
6778 if not c then Exit;
6780 c := False;
6781 for yy := y+1 to BOT_MAXJUMP do
6782 if not CollideLevel(Dist, -yy) then
6783 begin
6784 c := True;
6785 Break;
6786 end;
6788 if not c then Exit;
6790 c := False;
6791 for y := 0 to BOT_MAXJUMP do
6792 if CollideLevel(0, -y) then
6793 begin
6794 c := True;
6795 Break;
6796 end;
6798 if c then Exit;
6800 if y < yy then Exit;
6802 Result := True;
6803 end;
6805 function IsSafeTrigger(): Boolean;
6806 var
6807 a: Integer;
6808 begin
6809 Result := True;
6810 if gTriggers = nil then
6811 Exit;
6812 for a := 0 to High(gTriggers) do
6813 if Collide(gTriggers[a].X,
6814 gTriggers[a].Y,
6815 gTriggers[a].Width,
6816 gTriggers[a].Height) and
6817 (gTriggers[a].TriggerType in [TRIGGER_EXIT, TRIGGER_CLOSEDOOR,
6818 TRIGGER_CLOSETRAP, TRIGGER_TRAP,
6819 TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF,
6820 TRIGGER_ONOFF, TRIGGER_SPAWNMONSTER,
6821 TRIGGER_DAMAGE, TRIGGER_SHOT]) then
6822 Result := False;
6823 end;
6825 begin
6826 // Возможно, нажимаем кнопку:
6827 if Rnd(16) and IsSafeTrigger() then
6828 PressKey(KEY_OPEN);
6830 // Если под лифтом или ступеньками, то, возможно, прыгаем:
6831 if OnLadder() or ((BelowLadder() or BelowLiftUp()) and Rnd(8)) then
6832 begin
6833 ReleaseKey(KEY_LEFT);
6834 ReleaseKey(KEY_RIGHT);
6835 Jump();
6836 end;
6838 // Идем влево, если надо было:
6839 if GetAIFlag('GOLEFT') <> '' then
6840 begin
6841 RemoveAIFlag('GOLEFT');
6842 if CanRunLeft() then
6843 GoLeft(360);
6844 end;
6846 // Идем вправо, если надо было:
6847 if GetAIFlag('GORIGHT') <> '' then
6848 begin
6849 RemoveAIFlag('GORIGHT');
6850 if CanRunRight() then
6851 GoRight(360);
6852 end;
6854 // Если вылетели за карту, то пробуем вернуться:
6855 if FObj.X < -32 then
6856 GoRight(360)
6857 else
6858 if FObj.X+32 > gMapInfo.Width then
6859 GoLeft(360);
6861 // Прыгаем, если надо было:
6862 if GetAIFlag('NEEDJUMP') <> '' then
6863 begin
6864 Jump(0);
6865 RemoveAIFlag('NEEDJUMP');
6866 end;
6868 // Смотрим вверх, если надо было:
6869 if GetAIFlag('NEEDSEEUP') <> '' then
6870 begin
6871 ReleaseKey(KEY_UP);
6872 ReleaseKey(KEY_DOWN);
6873 PressKey(KEY_UP, 20);
6874 RemoveAIFlag('NEEDSEEUP');
6875 end;
6877 // Смотрим вниз, если надо было:
6878 if GetAIFlag('NEEDSEEDOWN') <> '' then
6879 begin
6880 ReleaseKey(KEY_UP);
6881 ReleaseKey(KEY_DOWN);
6882 PressKey(KEY_DOWN, 20);
6883 RemoveAIFlag('NEEDSEEDOWN');
6884 end;
6886 // Если нужно было в дыру и мы не на земле, то покорно летим:
6887 if GetAIFlag('GOINHOLE') <> '' then
6888 if not OnGround() then
6889 begin
6890 ReleaseKey(KEY_LEFT);
6891 ReleaseKey(KEY_RIGHT);
6892 RemoveAIFlag('GOINHOLE');
6893 SetAIFlag('FALLINHOLE', '1');
6894 end;
6896 // Если падали и достигли земли, то хватит падать:
6897 if GetAIFlag('FALLINHOLE') <> '' then
6898 if OnGround() then
6899 RemoveAIFlag('FALLINHOLE');
6901 // Если летели прямо и сейчас не на лестнице или на вершине лифта, то отходим в сторону:
6902 if not (KeyPressed(KEY_LEFT) or KeyPressed(KEY_RIGHT)) then
6903 if GetAIFlag('FALLINHOLE') = '' then
6904 if (not OnLadder()) or (FObj.Vel.Y >= 0) or (OnTopLift()) then
6905 if Rnd(2) then
6906 GoLeft(360)
6907 else
6908 GoRight(360);
6910 // Если на земле и можно подпрыгнуть, то, возможно, прыгаем:
6911 if OnGround() and
6912 CanJumpUp(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*32) and
6913 Rnd(8) then
6914 Jump();
6916 // Если на земле и возле дыры (глубина > 2 ростов игрока):
6917 if OnGround() and NearHole() then
6918 if NearDeepHole() then // Если это бездна
6919 case Random(6) of
6920 0..3: Turn(); // Бежим обратно
6921 4: Jump(); // Прыгаем
6922 5: begin // Прыгаем обратно
6923 Turn();
6924 Jump();
6925 end;
6926 end
6927 else // Это не бездна и мы еще не летим туда
6928 if GetAIFlag('GOINHOLE') = '' then
6929 case Random(6) of
6930 0: Turn(); // Не нужно туда
6931 1: Jump(); // Вдруг повезет - прыгаем
6932 else // Если яма с границей, то при случае можно туда прыгнуть
6933 if BorderHole() then
6934 SetAIFlag('GOINHOLE', '1');
6935 end;
6937 // Если на земле, но некуда идти:
6938 if (not CanRun()) and OnGround() then
6939 begin
6940 // Если мы на лестнице или можно перепрыгнуть, то прыгаем:
6941 if CanJumpOver() or OnLadder() then
6942 Jump()
6943 else // иначе попытаемся в другую сторону
6944 if Random(2) = 0 then
6945 begin
6946 if IsSafeTrigger() then
6947 PressKey(KEY_OPEN);
6948 end else
6949 Turn();
6950 end;
6952 // Осталось мало воздуха:
6953 if FAir < 36 * 2 then
6954 Jump(20);
6956 // Выбираемся из кислоты, если нет костюма, обожглись, или мало здоровья:
6957 if (FMegaRulez[MR_SUIT] < gTime) and ((FLastHit = HIT_ACID) or (Healthy() <= 1)) then
6958 if BodyInAcid(0, 0) then
6959 Jump();
6960 end;
6962 function TBot.FullInStep(XInc, YInc: Integer): Boolean;
6963 begin
6964 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
6965 PLAYER_RECT.Width, PLAYER_RECT.Height, PANEL_STEP, False);
6966 end;
6968 {function TBot.NeedItem(Item: Byte): Byte;
6969 begin
6970 Result := 4;
6971 end;}
6973 procedure TBot.SelectWeapon(Dist: Integer);
6974 var
6975 a: Integer;
6977 function HaveAmmo(weapon: Byte): Boolean;
6978 begin
6979 case weapon of
6980 WEAPON_PISTOL: Result := FAmmo[A_BULLETS] >= 1;
6981 WEAPON_SHOTGUN1: Result := FAmmo[A_SHELLS] >= 1;
6982 WEAPON_SHOTGUN2: Result := FAmmo[A_SHELLS] >= 2;
6983 WEAPON_CHAINGUN: Result := FAmmo[A_BULLETS] >= 10;
6984 WEAPON_ROCKETLAUNCHER: Result := FAmmo[A_ROCKETS] >= 1;
6985 WEAPON_PLASMA: Result := FAmmo[A_CELLS] >= 10;
6986 WEAPON_BFG: Result := FAmmo[A_CELLS] >= 40;
6987 WEAPON_SUPERPULEMET: Result := FAmmo[A_SHELLS] >= 1;
6988 WEAPON_FLAMETHROWER: Result := FAmmo[A_FUEL] >= 1;
6989 else Result := True;
6990 end;
6991 end;
6993 begin
6994 if Dist = -1 then Dist := BOT_LONGDIST;
6996 if Dist > BOT_LONGDIST then
6997 begin // Дальний бой
6998 for a := 0 to 9 do
6999 if FWeapon[FDifficult.WeaponPrior[a]] and HaveAmmo(FDifficult.WeaponPrior[a]) then
7000 begin
7001 FSelectedWeapon := FDifficult.WeaponPrior[a];
7002 Break;
7003 end;
7004 end
7005 else //if Dist > BOT_UNSAFEDIST then
7006 begin // Ближний бой
7007 for a := 0 to 9 do
7008 if FWeapon[FDifficult.CloseWeaponPrior[a]] and HaveAmmo(FDifficult.CloseWeaponPrior[a]) then
7009 begin
7010 FSelectedWeapon := FDifficult.CloseWeaponPrior[a];
7011 Break;
7012 end;
7013 end;
7014 { else
7015 begin
7016 for a := 0 to 9 do
7017 if FWeapon[FDifficult.SafeWeaponPrior[a]] and HaveAmmo(FDifficult.SafeWeaponPrior[a]) then
7018 begin
7019 FSelectedWeapon := FDifficult.SafeWeaponPrior[a];
7020 Break;
7021 end;
7022 end;}
7023 end;
7025 function TBot.PickItem(ItemType: Byte; force: Boolean; var remove: Boolean): Boolean;
7026 begin
7027 Result := inherited PickItem(ItemType, force, remove);
7029 if Result then SetAIFlag('SELECTWEAPON', '1');
7030 end;
7032 function TBot.Heal(value: Word; Soft: Boolean): Boolean;
7033 begin
7034 Result := inherited Heal(value, Soft);
7035 end;
7037 function TBot.Healthy(): Byte;
7038 begin
7039 if FMegaRulez[MR_INVUL] >= gTime then Result := 3
7040 else if (FHealth > 80) or ((FHealth > 50) and (FArmor > 20)) then Result := 3
7041 else if (FHealth > 50) then Result := 2
7042 else if (FHealth > 20) then Result := 1
7043 else Result := 0;
7044 end;
7046 function TBot.TargetOnScreen(TX, TY: Integer): Boolean;
7047 begin
7048 Result := (Abs(FObj.X-TX) <= Trunc(gPlayerScreenSize.X*0.6)) and
7049 (Abs(FObj.Y-TY) <= Trunc(gPlayerScreenSize.Y*0.6));
7050 end;
7052 procedure TBot.OnDamage(Angle: SmallInt);
7053 var
7054 pla: TPlayer;
7055 mon: TMonster;
7056 ok: Boolean;
7057 begin
7058 inherited;
7060 if (Angle = 0) or (Angle = 180) then
7061 begin
7062 ok := False;
7063 if (g_GetUIDType(FLastSpawnerUID) = UID_PLAYER) and
7064 LongBool(gGameSettings.Options and GAME_OPTION_BOTVSPLAYER) then
7065 begin // Игрок
7066 pla := g_Player_Get(FLastSpawnerUID);
7067 ok := not TargetOnScreen(pla.FObj.X + PLAYER_RECT.X,
7068 pla.FObj.Y + PLAYER_RECT.Y);
7069 end
7070 else
7071 if (g_GetUIDType(FLastSpawnerUID) = UID_MONSTER) and
7072 LongBool(gGameSettings.Options and GAME_OPTION_BOTVSMONSTER) then
7073 begin // Монстр
7074 mon := g_Monsters_ByUID(FLastSpawnerUID);
7075 ok := not TargetOnScreen(mon.Obj.X + mon.Obj.Rect.X,
7076 mon.Obj.Y + mon.Obj.Rect.Y);
7077 end;
7079 if ok then
7080 if Angle = 0 then
7081 SetAIFlag('ATTACKLEFT', '1')
7082 else
7083 SetAIFlag('ATTACKRIGHT', '1');
7084 end;
7085 end;
7087 function TBot.RunDirection(): TDirection;
7088 begin
7089 if Abs(Vel.X) >= 1 then
7090 begin
7091 if Vel.X > 0 then Result := TDirection.D_RIGHT else Result := TDirection.D_LEFT;
7092 end else
7093 Result := FDirection;
7094 end;
7096 function TBot.GetRnd(a: Byte): Boolean;
7097 begin
7098 if a = 0 then Result := False
7099 else if a = 255 then Result := True
7100 else Result := Random(256) > 255-a;
7101 end;
7103 function TBot.GetInterval(a: Byte; radius: SmallInt): SmallInt;
7104 begin
7105 Result := Round((255-a)/255*radius*(Random(2)-1));
7106 end;
7109 procedure TDifficult.save (st: TStream);
7110 begin
7111 utils.writeInt(st, Byte(DiagFire));
7112 utils.writeInt(st, Byte(InvisFire));
7113 utils.writeInt(st, Byte(DiagPrecision));
7114 utils.writeInt(st, Byte(FlyPrecision));
7115 utils.writeInt(st, Byte(Cover));
7116 utils.writeInt(st, Byte(CloseJump));
7117 st.WriteBuffer(WeaponPrior[Low(WeaponPrior)], sizeof(WeaponPrior));
7118 st.WriteBuffer(CloseWeaponPrior[Low(CloseWeaponPrior)], sizeof(CloseWeaponPrior));
7119 end;
7121 procedure TDifficult.load (st: TStream);
7122 begin
7123 DiagFire := utils.readByte(st);
7124 InvisFire := utils.readByte(st);
7125 DiagPrecision := utils.readByte(st);
7126 FlyPrecision := utils.readByte(st);
7127 Cover := utils.readByte(st);
7128 CloseJump := utils.readByte(st);
7129 st.ReadBuffer(WeaponPrior[Low(WeaponPrior)], sizeof(WeaponPrior));
7130 st.ReadBuffer(CloseWeaponPrior[Low(CloseWeaponPrior)], sizeof(CloseWeaponPrior));
7131 end;
7134 procedure TBot.SaveState (st: TStream);
7135 var
7136 i: Integer;
7137 dw: Integer;
7138 begin
7139 inherited SaveState(st);
7140 utils.writeSign(st, 'BOT0');
7141 // Выбранное оружие
7142 utils.writeInt(st, Byte(FSelectedWeapon));
7143 // UID цели
7144 utils.writeInt(st, Word(FTargetUID));
7145 // Время потери цели
7146 utils.writeInt(st, LongWord(FLastVisible));
7147 // Количество флагов ИИ
7148 dw := Length(FAIFlags);
7149 utils.writeInt(st, LongInt(dw));
7150 // Флаги ИИ
7151 for i := 0 to dw-1 do
7152 begin
7153 utils.writeStr(st, FAIFlags[i].Name, 20);
7154 utils.writeStr(st, FAIFlags[i].Value, 20);
7155 end;
7156 // Настройки сложности
7157 FDifficult.save(st);
7158 end;
7161 procedure TBot.LoadState (st: TStream);
7162 var
7163 i: Integer;
7164 dw: Integer;
7165 begin
7166 inherited LoadState(st);
7167 if not utils.checkSign(st, 'BOT0') then raise XStreamError.Create('invalid bot signature');
7168 // Выбранное оружие
7169 FSelectedWeapon := utils.readByte(st);
7170 // UID цели
7171 FTargetUID := utils.readWord(st);
7172 // Время потери цели
7173 FLastVisible := utils.readLongWord(st);
7174 // Количество флагов ИИ
7175 dw := utils.readLongInt(st);
7176 if (dw < 0) or (dw > 16384) then raise XStreamError.Create('invalid number of bot AI flags');
7177 SetLength(FAIFlags, dw);
7178 // Флаги ИИ
7179 for i := 0 to dw-1 do
7180 begin
7181 FAIFlags[i].Name := utils.readStr(st, 20);
7182 FAIFlags[i].Value := utils.readStr(st, 20);
7183 end;
7184 // Настройки сложности
7185 FDifficult.load(st);
7186 end;
7189 begin
7190 conRegVar('cheat_berserk_autoswitch', @gBerserkAutoswitch, 'autoswitch to fist when berserk pack taken', '', true, true);
7191 conRegVar('player_indicator', @gPlayerIndicator, 'Draw indicator only for current player, also for teammates, or not at all', 'Draw indicator only for current player, also for teammates, or not at all');
7192 conRegVar('player_indicator_style', @gPlayerIndicatorStyle, 'Visual appearance of indicator', 'Visual appearance of indicator');
7193 end.