DEADSOFTWARE

game: disable gfx for server
[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 {$IFDEF ENABLE_MENU}
631 g_menu,
632 {$ENDIF}
633 {$IFNDEF HEADLESS}
634 r_render,
635 {$ENDIF}
636 {$IFDEF ENABLE_GFX}
637 g_gfx,
638 {$ENDIF}
639 e_log, g_map, g_items, g_console, Math,
640 g_options, g_triggers, g_game, g_grid, e_res,
641 wadreader, g_monsters, CONFIG, g_language,
642 g_net, g_netmsg,
643 utils, xstreams;
645 const PLR_SAVE_VERSION = 0;
647 type
648 TBotProfile = record
649 name: ShortString;
650 model: ShortString;
651 team: Byte;
652 color: TRGB;
653 diag_fire: Byte;
654 invis_fire: Byte;
655 diag_precision: Byte;
656 fly_precision: Byte;
657 cover: Byte;
658 close_jump: Byte;
659 w_prior1: Array [WP_FIRST..WP_LAST] of Byte;
660 w_prior2: Array [WP_FIRST..WP_LAST] of Byte;
661 w_prior3: Array [WP_FIRST..WP_LAST] of Byte;
662 end;
664 const
665 TIME_RESPAWN1 = 1500;
666 TIME_RESPAWN2 = 2000;
667 TIME_RESPAWN3 = 3000;
668 PLAYER_SUIT_TIME = 30000;
669 PLAYER_INVUL_TIME = 30000;
670 PLAYER_INVIS_TIME = 35000;
671 FRAG_COMBO_TIME = 3000;
672 VEL_SW = 4;
673 VEL_FLY = 6;
674 PLAYER_HEADRECT: TRectWH = (X:24; Y:12; Width:20; Height:12);
675 BOT_MAXJUMP = 84;
676 BOT_LONGDIST = 300;
677 BOT_UNSAFEDIST = 128;
678 DIFFICULT_EASY: TDifficult = (DiagFire: 32; InvisFire: 32; DiagPrecision: 32;
679 FlyPrecision: 32; Cover: 32; CloseJump: 32;
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 DIFFICULT_MEDIUM: TDifficult = (DiagFire: 127; InvisFire: 127; DiagPrecision: 127;
682 FlyPrecision: 127; Cover: 127; CloseJump: 127;
683 WeaponPrior:(0,0,0,0,0,0,0,0,0,0,0); CloseWeaponPrior:(0,0,0,0,0,0,0,0,0,0,0));
684 DIFFICULT_HARD: TDifficult = (DiagFire: 255; InvisFire: 255; DiagPrecision: 255;
685 FlyPrecision: 255; Cover: 255; CloseJump: 255;
686 WeaponPrior:(0,0,0,0,0,0,0,0,0,0,0); CloseWeaponPrior:(0,0,0,0,0,0,0,0,0,0,0));
687 WEAPON_PRIOR1: Array [WP_FIRST..WP_LAST] of Byte =
688 (WEAPON_FLAMETHROWER, WEAPON_SUPERPULEMET,
689 WEAPON_SHOTGUN2, WEAPON_SHOTGUN1,
690 WEAPON_CHAINGUN, WEAPON_PLASMA, WEAPON_ROCKETLAUNCHER,
691 WEAPON_BFG, WEAPON_PISTOL, WEAPON_SAW, WEAPON_KASTET);
692 WEAPON_PRIOR2: Array [WP_FIRST..WP_LAST] of Byte =
693 (WEAPON_FLAMETHROWER, WEAPON_SUPERPULEMET,
694 WEAPON_BFG, WEAPON_ROCKETLAUNCHER,
695 WEAPON_SHOTGUN2, WEAPON_PLASMA, WEAPON_SHOTGUN1,
696 WEAPON_CHAINGUN, WEAPON_PISTOL, WEAPON_SAW, WEAPON_KASTET);
697 //WEAPON_PRIOR3: Array [WP_FIRST..WP_LAST] of Byte =
698 // (WEAPON_FLAMETHROWER, WEAPON_SUPERPULEMET,
699 // WEAPON_BFG, WEAPON_PLASMA, WEAPON_SHOTGUN2,
700 // WEAPON_CHAINGUN, WEAPON_SHOTGUN1, WEAPON_SAW,
701 // WEAPON_ROCKETLAUNCHER, WEAPON_PISTOL, WEAPON_KASTET);
702 WEAPON_RELOAD: Array [WP_FIRST..WP_LAST] of Byte =
703 (5, 2, 6, 18, 36, 2, 12, 2, 14, 2, 2);
705 PLAYER_SIGNATURE = $52594C50; // 'PLYR'
706 CORPSE_SIGNATURE = $50524F43; // 'CORP'
708 BOTNAMES_FILENAME = 'botnames.txt';
709 BOTLIST_FILENAME = 'botlist.txt';
711 var
712 MaxGibs: Word = 150;
713 MaxCorpses: Word = 20;
714 MaxShells: Word = 300;
715 CurrentGib: Integer = 0;
716 CurrentShell: Integer = 0;
717 BotNames: Array of String;
718 BotList: Array of TBotProfile;
719 SavedStates: Array of TPlayerSavedState;
722 function Lerp(X, Y, Factor: Integer): Integer;
723 begin
724 Result := X + ((Y - X) div Factor);
725 end;
727 function SameTeam(UID1, UID2: Word): Boolean;
728 begin
729 Result := False;
731 if (UID1 > UID_MAX_PLAYER) or (UID1 <= UID_MAX_GAME) or
732 (UID2 > UID_MAX_PLAYER) or (UID2 <= UID_MAX_GAME) then Exit;
734 if (g_Player_Get(UID1) = nil) or (g_Player_Get(UID2) = nil) then Exit;
736 if ((g_Player_Get(UID1).Team = TEAM_NONE) or
737 (g_Player_Get(UID2).Team = TEAM_NONE)) then Exit;
739 Result := g_Player_Get(UID1).FTeam = g_Player_Get(UID2).FTeam;
740 end;
742 procedure g_Gibs_SetMax(Count: Word);
743 begin
744 MaxGibs := Count;
745 SetLength(gGibs, Count);
747 if CurrentGib >= Count then
748 CurrentGib := 0;
749 end;
751 function g_Gibs_GetMax(): Word;
752 begin
753 Result := MaxGibs;
754 end;
756 procedure g_Shells_SetMax(Count: Word);
757 begin
758 MaxShells := Count;
759 SetLength(gShells, Count);
761 if CurrentShell >= Count then
762 CurrentShell := 0;
763 end;
765 function g_Shells_GetMax(): Word;
766 begin
767 Result := MaxShells;
768 end;
771 procedure g_Corpses_SetMax(Count: Word);
772 begin
773 MaxCorpses := Count;
774 SetLength(gCorpses, Count);
775 end;
777 function g_Corpses_GetMax(): Word;
778 begin
779 Result := MaxCorpses;
780 end;
782 function g_Player_Create(ModelName: String; Color: TRGB; Team: Byte; Bot: Boolean): Word;
783 var
784 a: Integer;
785 ok: Boolean;
786 begin
787 Result := 0;
789 ok := False;
790 a := 0;
792 // Есть ли место в gPlayers:
793 if gPlayers <> nil then
794 for a := 0 to High(gPlayers) do
795 if gPlayers[a] = nil then
796 begin
797 ok := True;
798 Break;
799 end;
801 // Нет места - расширяем gPlayers:
802 if not ok then
803 begin
804 SetLength(gPlayers, Length(gPlayers)+1);
805 a := High(gPlayers);
806 end;
808 // Создаем объект игрока:
809 if Bot then
810 gPlayers[a] := TBot.Create()
811 else
812 gPlayers[a] := TPlayer.Create();
815 gPlayers[a].FActualModelName := ModelName;
816 gPlayers[a].SetModel(ModelName);
818 // Нет модели - создание не возможно:
819 if gPlayers[a].FModel = nil then
820 begin
821 gPlayers[a].Free();
822 gPlayers[a] := nil;
823 g_FatalError(Format(_lc[I_GAME_ERROR_MODEL], [ModelName]));
824 Exit;
825 end;
827 if not (Team in [TEAM_RED, TEAM_BLUE]) then
828 if Random(2) = 0 then
829 Team := TEAM_RED
830 else
831 Team := TEAM_BLUE;
832 gPlayers[a].FPreferredTeam := Team;
834 case gGameSettings.GameMode of
835 GM_DM: gPlayers[a].FTeam := TEAM_NONE;
836 GM_TDM,
837 GM_CTF: gPlayers[a].FTeam := gPlayers[a].FPreferredTeam;
838 GM_SINGLE,
839 GM_COOP: gPlayers[a].FTeam := TEAM_COOP;
840 end;
842 // Если командная игра - красим модель в цвет команды:
843 gPlayers[a].FColor := Color;
844 if gPlayers[a].FTeam in [TEAM_RED, TEAM_BLUE] then
845 gPlayers[a].FModel.Color := TEAMCOLOR[gPlayers[a].FTeam]
846 else
847 gPlayers[a].FModel.Color := Color;
849 gPlayers[a].FUID := g_CreateUID(UID_PLAYER);
850 gPlayers[a].FAlive := False;
852 Result := gPlayers[a].FUID;
853 end;
855 function g_Player_CreateFromState (st: TStream): Word;
856 var
857 a, i: Integer;
858 ok, Bot: Boolean;
859 b: Byte;
860 begin
861 result := 0;
862 if (st = nil) then exit; //???
864 // Сигнатура игрока
865 if not utils.checkSign(st, 'PLYR') then raise XStreamError.Create('invalid player signature');
866 if (utils.readByte(st) <> PLR_SAVE_VERSION) then raise XStreamError.Create('invalid player version');
868 // Бот или человек:
869 Bot := utils.readBool(st);
871 ok := false;
872 a := 0;
874 // Есть ли место в gPlayers:
875 for a := 0 to High(gPlayers) do if (gPlayers[a] = nil) then begin ok := true; break; end;
877 // Нет места - расширяем gPlayers
878 if not ok then
879 begin
880 SetLength(gPlayers, Length(gPlayers)+1);
881 a := High(gPlayers);
882 end;
884 // Создаем объект игрока
885 if Bot then
886 gPlayers[a] := TBot.Create()
887 else
888 gPlayers[a] := TPlayer.Create();
889 gPlayers[a].FIamBot := Bot;
890 gPlayers[a].FPhysics := True;
892 // UID игрока
893 gPlayers[a].FUID := utils.readWord(st);
894 // Имя игрока
895 gPlayers[a].FName := utils.readStr(st);
896 // Команда
897 gPlayers[a].FTeam := utils.readByte(st);
898 gPlayers[a].FPreferredTeam := gPlayers[a].FTeam;
899 // Жив ли
900 gPlayers[a].FAlive := utils.readBool(st);
901 // Израсходовал ли все жизни
902 gPlayers[a].FNoRespawn := utils.readBool(st);
903 // Направление
904 b := utils.readByte(st);
905 if b = 1 then gPlayers[a].FDirection := TDirection.D_LEFT else gPlayers[a].FDirection := TDirection.D_RIGHT; // b = 2
906 // Здоровье
907 gPlayers[a].FHealth := utils.readLongInt(st);
908 // Фора
909 gPlayers[a].FHandicap := utils.readLongInt(st);
910 // Жизни
911 gPlayers[a].FLives := utils.readByte(st);
912 // Броня
913 gPlayers[a].FArmor := utils.readLongInt(st);
914 // Запас воздуха
915 gPlayers[a].FAir := utils.readLongInt(st);
916 // Запас горючего
917 gPlayers[a].FJetFuel := utils.readLongInt(st);
918 // Боль
919 gPlayers[a].FPain := utils.readLongInt(st);
920 // Убил
921 gPlayers[a].FKills := utils.readLongInt(st);
922 // Убил монстров
923 gPlayers[a].FMonsterKills := utils.readLongInt(st);
924 // Фрагов
925 gPlayers[a].FFrags := utils.readLongInt(st);
926 // Фрагов подряд
927 gPlayers[a].FFragCombo := utils.readByte(st);
928 // Время последнего фрага
929 gPlayers[a].FLastFrag := utils.readLongWord(st);
930 // Смертей
931 gPlayers[a].FDeath := utils.readLongInt(st);
932 // Какой флаг несет
933 gPlayers[a].FFlag := utils.readByte(st);
934 // Нашел секретов
935 gPlayers[a].FSecrets := utils.readLongInt(st);
936 // Текущее оружие
937 gPlayers[a].FCurrWeap := utils.readByte(st);
938 // Следующее желаемое оружие
939 gPlayers[a].FNextWeap := utils.readWord(st);
940 // ...и пауза
941 gPlayers[a].FNextWeapDelay := utils.readByte(st);
942 // Время зарядки BFG
943 gPlayers[a].FBFGFireCounter := utils.readSmallInt(st);
944 // Буфер урона
945 gPlayers[a].FDamageBuffer := utils.readLongInt(st);
946 // Последний ударивший
947 gPlayers[a].FLastSpawnerUID := utils.readWord(st);
948 // Тип последнего полученного урона
949 gPlayers[a].FLastHit := utils.readByte(st);
950 // Объект игрока:
951 Obj_LoadState(@gPlayers[a].FObj, st);
952 // Текущее количество патронов
953 for i := A_BULLETS to A_HIGH do gPlayers[a].FAmmo[i] := utils.readWord(st);
954 // Максимальное количество патронов
955 for i := A_BULLETS to A_HIGH do gPlayers[a].FMaxAmmo[i] := utils.readWord(st);
956 // Наличие оружия
957 for i := WP_FIRST to WP_LAST do gPlayers[a].FWeapon[i] := utils.readBool(st);
958 // Время перезарядки оружия
959 for i := WP_FIRST to WP_LAST do gPlayers[a].FReloading[i] := utils.readWord(st);
960 // Наличие рюкзака
961 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_ITEM_BACKPACK);
962 // Наличие красного ключа
963 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_KEY_RED);
964 // Наличие зеленого ключа
965 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_KEY_GREEN);
966 // Наличие синего ключа
967 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_KEY_BLUE);
968 // Наличие берсерка
969 if utils.readBool(st) then Include(gPlayers[a].FRulez, R_BERSERK);
970 // Время действия специальных предметов
971 for i := MR_SUIT to MR_MAX do gPlayers[a].FMegaRulez[i] := utils.readLongWord(st);
972 // Время до повторного респауна, смены оружия, исользования, захвата флага
973 for i := T_RESPAWN to T_FLAGCAP do gPlayers[a].FTime[i] := utils.readLongWord(st);
975 // Название модели:
976 gPlayers[a].FActualModelName := utils.readStr(st);
977 // Цвет модели
978 gPlayers[a].FColor.R := utils.readByte(st);
979 gPlayers[a].FColor.G := utils.readByte(st);
980 gPlayers[a].FColor.B := utils.readByte(st);
981 // Обновляем модель игрока
982 gPlayers[a].SetModel(gPlayers[a].FActualModelName);
984 // Нет модели - создание невозможно
985 if (gPlayers[a].FModel = nil) then
986 begin
987 gPlayers[a].Free();
988 gPlayers[a] := nil;
989 g_FatalError(Format(_lc[I_GAME_ERROR_MODEL], [gPlayers[a].FActualModelName]));
990 exit;
991 end;
993 // Если командная игра - красим модель в цвет команды
994 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
995 gPlayers[a].FModel.Color := TEAMCOLOR[gPlayers[a].FTeam]
996 else
997 gPlayers[a].FModel.Color := gPlayers[a].FColor;
999 result := gPlayers[a].FUID;
1000 end;
1003 procedure g_Player_ResetTeams();
1004 var
1005 a: Integer;
1006 begin
1007 if g_Game_IsClient then
1008 Exit;
1009 if gPlayers = nil then
1010 Exit;
1011 for a := Low(gPlayers) to High(gPlayers) do
1012 if gPlayers[a] <> nil then
1013 case gGameSettings.GameMode of
1014 GM_DM:
1015 gPlayers[a].ChangeTeam(TEAM_NONE);
1016 GM_TDM, GM_CTF:
1017 if not (gPlayers[a].Team in [TEAM_RED, TEAM_BLUE]) then
1018 if gPlayers[a].FPreferredTeam in [TEAM_RED, TEAM_BLUE] then
1019 gPlayers[a].ChangeTeam(gPlayers[a].FPreferredTeam)
1020 else
1021 if a mod 2 = 0 then
1022 gPlayers[a].ChangeTeam(TEAM_RED)
1023 else
1024 gPlayers[a].ChangeTeam(TEAM_BLUE);
1025 GM_SINGLE,
1026 GM_COOP:
1027 gPlayers[a].ChangeTeam(TEAM_COOP);
1028 end;
1029 end;
1031 procedure g_Bot_Add(Team, Difficult: Byte; Handicap: Integer = 100);
1032 var
1033 m: SSArray;
1034 _name, _model: String;
1035 a, tr, tb: Integer;
1036 begin
1037 if not g_Game_IsServer then Exit;
1039 // Список названий моделей:
1040 m := g_PlayerModel_GetNames();
1041 if m = nil then
1042 Exit;
1044 // Команда:
1045 if (gGameSettings.GameType = GT_SINGLE) or (gGameSettings.GameMode = GM_COOP) then
1046 Team := TEAM_COOP // COOP
1047 else
1048 if gGameSettings.GameMode = GM_DM then
1049 Team := TEAM_NONE // DM
1050 else
1051 if Team = TEAM_NONE then // CTF / TDM
1052 begin
1053 // Автобаланс команд:
1054 tr := 0;
1055 tb := 0;
1057 for a := 0 to High(gPlayers) do
1058 if gPlayers[a] <> nil then
1059 begin
1060 if gPlayers[a].Team = TEAM_RED then
1061 Inc(tr)
1062 else
1063 if gPlayers[a].Team = TEAM_BLUE then
1064 Inc(tb);
1065 end;
1067 if tr > tb then
1068 Team := TEAM_BLUE
1069 else
1070 if tb > tr then
1071 Team := TEAM_RED
1072 else // tr = tb
1073 if Random(2) = 0 then
1074 Team := TEAM_RED
1075 else
1076 Team := TEAM_BLUE;
1077 end;
1079 // Выбираем боту имя:
1080 _name := '';
1081 if BotNames <> nil then
1082 for a := 0 to High(BotNames) do
1083 if g_Player_ValidName(BotNames[a]) then
1084 begin
1085 _name := BotNames[a];
1086 Break;
1087 end;
1089 // Выбираем случайную модель:
1090 _model := m[Random(Length(m))];
1092 // Создаем бота:
1093 with g_Player_Get(g_Player_Create(_model,
1094 _RGB(Min(Random(9)*32, 255),
1095 Min(Random(9)*32, 255),
1096 Min(Random(9)*32, 255)),
1097 Team, True)) as TBot do
1098 begin
1099 // Если имени нет, делаем его из UID бота
1100 if _name = '' then
1101 Name := Format('DFBOT%.5d', [UID])
1102 else
1103 Name := _name;
1105 case Difficult of
1106 1: FDifficult := DIFFICULT_EASY;
1107 2: FDifficult := DIFFICULT_MEDIUM;
1108 else FDifficult := DIFFICULT_HARD;
1109 end;
1111 for a := WP_FIRST to WP_LAST do
1112 begin
1113 FDifficult.WeaponPrior[a] := WEAPON_PRIOR1[a];
1114 FDifficult.CloseWeaponPrior[a] := WEAPON_PRIOR2[a];
1115 //FDifficult.SafeWeaponPrior[a] := WEAPON_PRIOR3[a];
1116 end;
1118 FHandicap := Handicap;
1120 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [Name]), True);
1122 if g_Game_IsNet then MH_SEND_PlayerCreate(UID);
1123 if g_Game_IsServer and (gGameSettings.MaxLives > 0) then
1124 Spectate();
1125 end;
1126 end;
1128 procedure g_Bot_AddList(Team: Byte; lName: ShortString; num: Integer = -1; Handicap: Integer = 100);
1129 var
1130 m: SSArray;
1131 _name, _model: String;
1132 a: Integer;
1133 begin
1134 if not g_Game_IsServer then Exit;
1136 // Список названий моделей:
1137 m := g_PlayerModel_GetNames();
1138 if m = nil then
1139 Exit;
1141 // Команда:
1142 if (gGameSettings.GameType = GT_SINGLE) or (gGameSettings.GameMode = GM_COOP) then
1143 Team := TEAM_COOP // COOP
1144 else
1145 if gGameSettings.GameMode = GM_DM then
1146 Team := TEAM_NONE // DM
1147 else
1148 if Team = TEAM_NONE then
1149 Team := BotList[num].team; // CTF / TDM
1151 // Выбираем настройки бота из списка по номеру или имени:
1152 lName := AnsiLowerCase(lName);
1153 if (num < 0) or (num > Length(BotList)-1) then
1154 num := -1;
1155 if (num = -1) and (lName <> '') and (BotList <> nil) then
1156 for a := 0 to High(BotList) do
1157 if AnsiLowerCase(BotList[a].name) = lName then
1158 begin
1159 num := a;
1160 Break;
1161 end;
1162 if num = -1 then
1163 Exit;
1165 // Имя бота:
1166 _name := BotList[num].name;
1167 // Занято - выбираем случайное:
1168 if not g_Player_ValidName(_name) then
1169 repeat
1170 _name := Format('DFBOT%.2d', [Random(100)]);
1171 until g_Player_ValidName(_name);
1173 // Модель:
1174 _model := BotList[num].model;
1175 // Нет такой - выбираем случайную:
1176 if not InSArray(_model, m) then
1177 _model := m[Random(Length(m))];
1179 // Создаем бота:
1180 with g_Player_Get(g_Player_Create(_model, BotList[num].color, Team, True)) as TBot do
1181 begin
1182 Name := _name;
1184 FDifficult.DiagFire := BotList[num].diag_fire;
1185 FDifficult.InvisFire := BotList[num].invis_fire;
1186 FDifficult.DiagPrecision := BotList[num].diag_precision;
1187 FDifficult.FlyPrecision := BotList[num].fly_precision;
1188 FDifficult.Cover := BotList[num].cover;
1189 FDifficult.CloseJump := BotList[num].close_jump;
1191 FHandicap := Handicap;
1193 for a := WP_FIRST to WP_LAST do
1194 begin
1195 FDifficult.WeaponPrior[a] := BotList[num].w_prior1[a];
1196 FDifficult.CloseWeaponPrior[a] := BotList[num].w_prior2[a];
1197 //FDifficult.SafeWeaponPrior[a] := BotList[num].w_prior3[a];
1198 end;
1200 g_Console_Add(Format(_lc[I_PLAYER_JOIN], [Name]), True);
1202 if g_Game_IsNet then MH_SEND_PlayerCreate(UID);
1203 end;
1204 end;
1206 procedure g_Bot_RemoveAll();
1207 var
1208 a: Integer;
1209 begin
1210 if not g_Game_IsServer then Exit;
1211 if gPlayers = nil then Exit;
1213 for a := 0 to High(gPlayers) do
1214 if gPlayers[a] <> nil then
1215 if gPlayers[a] is TBot then
1216 begin
1217 gPlayers[a].Lives := 0;
1218 gPlayers[a].Kill(K_SIMPLEKILL, 0, HIT_DISCON);
1219 g_Console_Add(Format(_lc[I_PLAYER_LEAVE], [gPlayers[a].Name]), True);
1220 g_Player_Remove(gPlayers[a].FUID);
1221 end;
1223 g_Bot_MixNames();
1224 end;
1226 procedure g_Bot_MixNames();
1227 var
1228 s: String;
1229 a, b: Integer;
1230 begin
1231 if BotNames <> nil then
1232 for a := 0 to High(BotNames) do
1233 begin
1234 b := Random(Length(BotNames));
1235 s := BotNames[a];
1236 Botnames[a] := BotNames[b];
1237 BotNames[b] := s;
1238 end;
1239 end;
1241 procedure g_Player_Remove(UID: Word);
1242 var
1243 i: Integer;
1244 begin
1245 if gPlayers = nil then Exit;
1247 if g_Game_IsServer and g_Game_IsNet then
1248 MH_SEND_PlayerDelete(UID);
1250 for i := 0 to High(gPlayers) do
1251 if gPlayers[i] <> nil then
1252 if gPlayers[i].FUID = UID then
1253 begin
1254 if gPlayers[i] is TPlayer then
1255 TPlayer(gPlayers[i]).Free()
1256 else
1257 TBot(gPlayers[i]).Free();
1258 gPlayers[i] := nil;
1259 Exit;
1260 end;
1261 end;
1263 procedure g_Player_Init();
1264 var
1265 F: TextFile;
1266 s: String;
1267 a, b: Integer;
1268 config: TConfig;
1269 sa: SSArray;
1270 path: AnsiString;
1271 begin
1272 BotNames := nil;
1274 path := BOTNAMES_FILENAME;
1275 if e_FindResource(DataDirs, path) = false then
1276 Exit;
1278 // Читаем возможные имена ботов из файла:
1279 AssignFile(F, path);
1280 Reset(F);
1282 while not EOF(F) do
1283 begin
1284 ReadLn(F, s);
1286 s := Trim(s);
1287 if s = '' then
1288 Continue;
1290 SetLength(BotNames, Length(BotNames)+1);
1291 BotNames[High(BotNames)] := s;
1292 end;
1294 CloseFile(F);
1296 // Перемешиваем их:
1297 g_Bot_MixNames();
1299 // Читаем файл с параметрами ботов:
1300 config := TConfig.CreateFile(path);
1301 BotList := nil;
1302 a := 0;
1304 while config.SectionExists(IntToStr(a)) do
1305 begin
1306 SetLength(BotList, Length(BotList)+1);
1308 with BotList[High(BotList)] do
1309 begin
1310 // Имя бота:
1311 name := config.ReadStr(IntToStr(a), 'name', '');
1312 // Модель:
1313 model := config.ReadStr(IntToStr(a), 'model', '');
1314 // Команда:
1315 if config.ReadStr(IntToStr(a), 'team', 'red') = 'red' then
1316 team := TEAM_RED
1317 else
1318 team := TEAM_BLUE;
1319 // Цвет модели:
1320 sa := parse(config.ReadStr(IntToStr(a), 'color', ''));
1321 color.R := StrToIntDef(sa[0], 0);
1322 color.G := StrToIntDef(sa[1], 0);
1323 color.B := StrToIntDef(sa[2], 0);
1324 // Вероятность стрельбы под углом:
1325 diag_fire := config.ReadInt(IntToStr(a), 'diag_fire', 0);
1326 // Вероятность ответного огня по невидимому сопернику:
1327 invis_fire := config.ReadInt(IntToStr(a), 'invis_fire', 0);
1328 // Точность стрельбы под углом:
1329 diag_precision := config.ReadInt(IntToStr(a), 'diag_precision', 0);
1330 // Точность стрельбы в полете:
1331 fly_precision := config.ReadInt(IntToStr(a), 'fly_precision', 0);
1332 // Точность уклонения от снарядов:
1333 cover := config.ReadInt(IntToStr(a), 'cover', 0);
1334 // Вероятность прыжка при приближении соперника:
1335 close_jump := config.ReadInt(IntToStr(a), 'close_jump', 0);
1336 // Приоритеты оружия для дальнего боя:
1337 sa := parse(config.ReadStr(IntToStr(a), 'w_prior1', ''));
1338 if Length(sa) = 10 then
1339 for b := 0 to 9 do
1340 w_prior1[b] := EnsureRange(StrToInt(sa[b]), 0, 9);
1341 // Приоритеты оружия для ближнего боя:
1342 sa := parse(config.ReadStr(IntToStr(a), 'w_prior2', ''));
1343 if Length(sa) = 10 then
1344 for b := 0 to 9 do
1345 w_prior2[b] := EnsureRange(StrToInt(sa[b]), 0, 9);
1347 {sa := parse(config.ReadStr(IntToStr(a), 'w_prior3', ''));
1348 if Length(sa) = 10 then
1349 for b := 0 to 9 do
1350 w_prior3[b] := EnsureRange(StrToInt(sa[b]), 0, 9);}
1351 end;
1353 a := a + 1;
1354 end;
1356 config.Free();
1357 SetLength(SavedStates, 0);
1358 end;
1360 procedure g_Player_Free();
1361 var
1362 i: Integer;
1363 begin
1364 if gPlayers <> nil then
1365 begin
1366 for i := 0 to High(gPlayers) do
1367 if gPlayers[i] <> nil then
1368 begin
1369 if gPlayers[i] is TPlayer then
1370 TPlayer(gPlayers[i]).Free()
1371 else
1372 TBot(gPlayers[i]).Free();
1373 gPlayers[i] := nil;
1374 end;
1376 gPlayers := nil;
1377 end;
1379 gPlayer1 := nil;
1380 gPlayer2 := nil;
1381 SetLength(SavedStates, 0);
1382 end;
1384 procedure g_Player_PreUpdate();
1385 var
1386 i: Integer;
1387 begin
1388 if gPlayers = nil then Exit;
1389 for i := 0 to High(gPlayers) do
1390 if gPlayers[i] <> nil then
1391 gPlayers[i].PreUpdate();
1392 end;
1394 procedure g_Player_UpdateAll();
1395 var
1396 i: Integer;
1397 begin
1398 if gPlayers = nil then Exit;
1400 //e_WriteLog('***g_Player_UpdateAll: ENTER', MSG_WARNING);
1401 for i := 0 to High(gPlayers) do
1402 begin
1403 if gPlayers[i] <> nil then
1404 begin
1405 if gPlayers[i] is TPlayer then
1406 begin
1407 gPlayers[i].Update();
1408 gPlayers[i].RealizeCurrentWeapon(); // WARNING! DO NOT MOVE THIS INTO `Update()`!
1409 end
1410 else
1411 begin
1412 // bot updates weapons in `UpdateCombat()`
1413 TBot(gPlayers[i]).Update();
1414 end;
1415 end;
1416 end;
1417 //e_WriteLog('***g_Player_UpdateAll: EXIT', MSG_WARNING);
1418 end;
1420 function g_Player_Get(UID: Word): TPlayer;
1421 var
1422 a: Integer;
1423 begin
1424 Result := nil;
1426 if gPlayers = nil then
1427 Exit;
1429 for a := 0 to High(gPlayers) do
1430 if gPlayers[a] <> nil then
1431 if gPlayers[a].FUID = UID then
1432 begin
1433 Result := gPlayers[a];
1434 Exit;
1435 end;
1436 end;
1438 function g_Player_GetCount(): Byte;
1439 var
1440 a: Integer;
1441 begin
1442 Result := 0;
1444 if gPlayers = nil then
1445 Exit;
1447 for a := 0 to High(gPlayers) do
1448 if gPlayers[a] <> nil then
1449 Result := Result + 1;
1450 end;
1452 function g_Player_GetStats(): TPlayerStatArray;
1453 var
1454 a: Integer;
1455 begin
1456 Result := nil;
1458 if gPlayers = nil then Exit;
1460 for a := 0 to High(gPlayers) do
1461 if gPlayers[a] <> nil then
1462 begin
1463 SetLength(Result, Length(Result)+1);
1464 with Result[High(Result)] do
1465 begin
1466 Num := a;
1467 Ping := gPlayers[a].FPing;
1468 Loss := gPlayers[a].FLoss;
1469 Name := gPlayers[a].FName;
1470 Team := gPlayers[a].FTeam;
1471 Frags := gPlayers[a].FFrags;
1472 Deaths := gPlayers[a].FDeath;
1473 Kills := gPlayers[a].FKills;
1474 Color := gPlayers[a].FModel.Color;
1475 Lives := gPlayers[a].FLives;
1476 Spectator := gPlayers[a].FSpectator;
1477 UID := gPlayers[a].FUID;
1478 end;
1479 end;
1480 end;
1482 procedure g_Player_ResetReady();
1483 var
1484 a: Integer;
1485 begin
1486 if not g_Game_IsServer then Exit;
1487 if gPlayers = nil then Exit;
1489 for a := 0 to High(gPlayers) do
1490 if gPlayers[a] <> nil then
1491 begin
1492 gPlayers[a].FReady := False;
1493 if g_Game_IsNet then
1494 MH_SEND_GameEvent(NET_EV_INTER_READY, gPlayers[a].UID, 'N');
1495 end;
1496 end;
1498 procedure g_Player_RememberAll;
1499 var
1500 i: Integer;
1501 begin
1502 for i := Low(gPlayers) to High(gPlayers) do
1503 if (gPlayers[i] <> nil) and gPlayers[i].alive then
1504 gPlayers[i].RememberState;
1505 end;
1507 procedure g_Player_ResetAll(Force, Silent: Boolean);
1508 var
1509 i: Integer;
1510 begin
1511 gTeamStat[TEAM_RED].Goals := 0;
1512 gTeamStat[TEAM_BLUE].Goals := 0;
1514 if gPlayers <> nil then
1515 for i := 0 to High(gPlayers) do
1516 if gPlayers[i] <> nil then
1517 begin
1518 gPlayers[i].Reset(Force);
1520 if gPlayers[i] is TPlayer then
1521 begin
1522 if (not gPlayers[i].FSpectator) or gPlayers[i].FWantsInGame then
1523 gPlayers[i].Respawn(Silent)
1524 else
1525 gPlayers[i].Spectate();
1526 end
1527 else
1528 TBot(gPlayers[i]).Respawn(Silent);
1529 end;
1530 end;
1532 function g_Player_CreateCorpse(Player: TPlayer): Integer;
1533 var
1534 i: Integer;
1535 find_id: DWORD;
1536 ok: Boolean;
1537 begin
1538 Result := -1;
1540 if Player.alive then
1541 Exit;
1543 // Разрываем связь с прежним трупом:
1544 i := Player.FCorpse;
1545 if (i >= 0) and (i < Length(gCorpses)) then
1546 begin
1547 if (gCorpses[i] <> nil) and (gCorpses[i].FPlayerUID = Player.FUID) then
1548 gCorpses[i].FPlayerUID := 0;
1549 end;
1551 if Player.FObj.Y >= gMapInfo.Height+128 then
1552 Exit;
1554 with Player do
1555 begin
1556 if (FHealth >= -50) or (gGibsCount = 0) then
1557 begin
1558 if (gCorpses = nil) or (Length(gCorpses) = 0) then
1559 Exit;
1561 ok := False;
1562 for find_id := 0 to High(gCorpses) do
1563 if gCorpses[find_id] = nil then
1564 begin
1565 ok := True;
1566 Break;
1567 end;
1569 if not ok then
1570 find_id := Random(Length(gCorpses));
1572 gCorpses[find_id] := TCorpse.Create(FObj.X, FObj.Y, FModel.GetName(), FHealth < -20);
1573 gCorpses[find_id].FModel.Color := FModel.Color;
1574 gCorpses[find_id].FObj.Vel := FObj.Vel;
1575 gCorpses[find_id].FObj.Accel := FObj.Accel;
1576 gCorpses[find_id].FPlayerUID := FUID;
1578 Result := find_id;
1579 end
1580 else
1581 g_Player_CreateGibs(FObj.X + PLAYER_RECT_CX, FObj.Y + PLAYER_RECT_CY, FModel.id, FModel.Color);
1582 end;
1583 end;
1585 procedure g_Player_CreateShell(fX, fY, dX, dY: Integer; T: Byte);
1586 begin
1587 if (gShells = nil) or (Length(gShells) = 0) then
1588 Exit;
1590 with gShells[CurrentShell] do
1591 begin
1592 g_Obj_Init(@Obj);
1593 Obj.Rect.X := 0;
1594 Obj.Rect.Y := 0;
1595 if T = SHELL_BULLET then
1596 begin
1597 Obj.Rect.Width := 4;
1598 Obj.Rect.Height := 2;
1599 end
1600 else
1601 begin
1602 Obj.Rect.Width := 7;
1603 Obj.Rect.Height := 3;
1604 end;
1605 SType := T;
1606 alive := True;
1607 Obj.X := fX;
1608 Obj.Y := fY;
1609 g_Obj_Push(@Obj, dX + Random(4)-Random(4), dY-Random(4));
1610 positionChanged(); // this updates spatial accelerators
1611 RAngle := Random(360);
1612 Timeout := gTime + SHELL_TIMEOUT;
1614 if CurrentShell >= High(gShells) then
1615 CurrentShell := 0
1616 else
1617 Inc(CurrentShell);
1618 end;
1619 end;
1621 procedure g_Player_CreateGibs (fX, fY, mid: Integer; fColor: TRGB);
1622 var
1623 a: Integer;
1624 GibsArray: TGibsArray;
1625 {$IFDEF ENABLE_GFX}
1626 Blood: TModelBlood;
1627 {$ENDIF}
1628 begin
1629 if mid = -1 then
1630 Exit;
1631 if (gGibs = nil) or (Length(gGibs) = 0) then
1632 Exit;
1633 if not g_PlayerModel_GetGibs(mid, GibsArray) then
1634 Exit;
1636 {$IFDEF ENABLE_GFX}
1637 Blood := PlayerModelsArray[mid].Blood;
1638 {$ENDIF}
1640 for a := 0 to High(GibsArray) do
1641 with gGibs[CurrentGib] do
1642 begin
1643 ModelID := mid;
1644 GibID := GibsArray[a];
1645 Color := fColor;
1646 alive := True;
1647 g_Obj_Init(@Obj);
1648 {$IFNDEF HEADLESS}
1649 Obj.Rect := r_Render_GetGibRect(ModelID, GibID);
1650 {$ELSE}
1651 Obj.Rect.X := 16;
1652 Obj.Rect.Y := 16;
1653 Obj.Rect.Width := 16;
1654 Obj.Rect.Height := 16;
1655 {$ENDIF}
1656 Obj.X := fX - Obj.Rect.X - (Obj.Rect.Width div 2);
1657 Obj.Y := fY - Obj.Rect.Y - (Obj.Rect.Height div 2);
1658 g_Obj_PushA(@Obj, 25 + Random(10), Random(361));
1659 positionChanged(); // this updates spatial accelerators
1660 RAngle := Random(360);
1662 {$IFDEF ENABLE_GFX}
1663 if gBloodCount > 0 then
1664 begin
1665 g_GFX_Blood(
1666 fX,
1667 fY,
1668 16 * gBloodCount + Random(5 * gBloodCount),
1669 -16 + Random(33),
1670 -16 + Random(33),
1671 Random(48),
1672 Random(48),
1673 Blood.R,
1674 Blood.G,
1675 Blood.B,
1676 Blood.Kind
1677 );
1678 end;
1679 {$ENDIF}
1681 if CurrentGib >= High(gGibs) then
1682 CurrentGib := 0
1683 else
1684 Inc(CurrentGib);
1685 end;
1686 end;
1688 procedure g_Player_UpdatePhysicalObjects();
1689 var
1690 i: Integer;
1691 vel: TPoint2i;
1692 mr: Word;
1694 procedure ShellSound_Bounce(X, Y: Integer; T: Byte);
1695 var
1696 k: Integer;
1697 begin
1698 k := 1 + Random(2);
1699 if T = SHELL_BULLET then
1700 g_Sound_PlayExAt('SOUND_PLAYER_CASING' + IntToStr(k), X, Y)
1701 else
1702 g_Sound_PlayExAt('SOUND_PLAYER_SHELL' + IntToStr(k), X, Y);
1703 end;
1705 begin
1706 // Куски мяса:
1707 if gGibs <> nil then
1708 for i := 0 to High(gGibs) do
1709 if gGibs[i].alive then
1710 with gGibs[i] do
1711 begin
1712 Obj.oldX := Obj.X;
1713 Obj.oldY := Obj.Y;
1715 vel := Obj.Vel;
1716 mr := g_Obj_Move(@Obj, True, False, True);
1717 positionChanged(); // this updates spatial accelerators
1719 if WordBool(mr and MOVE_FALLOUT) then
1720 begin
1721 alive := False;
1722 Continue;
1723 end;
1725 // Отлетает от удара о стену/потолок/пол:
1726 if WordBool(mr and MOVE_HITWALL) then
1727 Obj.Vel.X := -(vel.X div 2);
1728 if WordBool(mr and (MOVE_HITCEIL or MOVE_HITLAND)) then
1729 Obj.Vel.Y := -(vel.Y div 2);
1731 if (Obj.Vel.X >= 0) then
1732 begin // Clockwise
1733 RAngle := RAngle + Abs(Obj.Vel.X)*6 + Abs(Obj.Vel.Y);
1734 if RAngle >= 360 then
1735 RAngle := RAngle mod 360;
1736 end else begin // Counter-clockwise
1737 RAngle := RAngle - Abs(Obj.Vel.X)*6 - Abs(Obj.Vel.Y);
1738 if RAngle < 0 then
1739 RAngle := (360 - (Abs(RAngle) mod 360)) mod 360;
1740 end;
1742 // Сопротивление воздуха для куска трупа:
1743 if gTime mod (GAME_TICK*3) = 0 then
1744 Obj.Vel.X := z_dec(Obj.Vel.X, 1);
1745 end;
1747 // Трупы:
1748 if gCorpses <> nil then
1749 for i := 0 to High(gCorpses) do
1750 if gCorpses[i] <> nil then
1751 if gCorpses[i].State = CORPSE_STATE_REMOVEME then
1752 begin
1753 gCorpses[i].Free();
1754 gCorpses[i] := nil;
1755 end
1756 else
1757 gCorpses[i].Update();
1759 // Гильзы:
1760 if gShells <> nil then
1761 for i := 0 to High(gShells) do
1762 if gShells[i].alive then
1763 with gShells[i] do
1764 begin
1765 Obj.oldX := Obj.X;
1766 Obj.oldY := Obj.Y;
1768 vel := Obj.Vel;
1769 mr := g_Obj_Move(@Obj, True, False, True);
1770 positionChanged(); // this updates spatial accelerators
1772 if WordBool(mr and MOVE_FALLOUT) or (gShells[i].Timeout < gTime) then
1773 begin
1774 alive := False;
1775 Continue;
1776 end;
1778 // Отлетает от удара о стену/потолок/пол:
1779 if WordBool(mr and MOVE_HITWALL) then
1780 begin
1781 Obj.Vel.X := -(vel.X div 2);
1782 if not WordBool(mr and MOVE_INWATER) then
1783 ShellSound_Bounce(Obj.X, Obj.Y, SType);
1784 end;
1785 if WordBool(mr and (MOVE_HITCEIL or MOVE_HITLAND)) then
1786 begin
1787 Obj.Vel.Y := -(vel.Y div 2);
1788 if Obj.Vel.X <> 0 then Obj.Vel.X := Obj.Vel.X div 2;
1789 if (Obj.Vel.X = 0) and (Obj.Vel.Y = 0) then
1790 begin
1791 if RAngle mod 90 <> 0 then
1792 RAngle := (RAngle div 90) * 90;
1793 end
1794 else if not WordBool(mr and MOVE_INWATER) then
1795 ShellSound_Bounce(Obj.X, Obj.Y, SType);
1796 end;
1798 if (Obj.Vel.X >= 0) then
1799 begin // Clockwise
1800 RAngle := RAngle + Abs(Obj.Vel.X)*8 + Abs(Obj.Vel.Y);
1801 if RAngle >= 360 then
1802 RAngle := RAngle mod 360;
1803 end else begin // Counter-clockwise
1804 RAngle := RAngle - Abs(Obj.Vel.X)*8 - Abs(Obj.Vel.Y);
1805 if RAngle < 0 then
1806 RAngle := (360 - (Abs(RAngle) mod 360)) mod 360;
1807 end;
1808 end;
1809 end;
1812 procedure TGib.getMapBox (out x, y, w, h: Integer); inline;
1813 begin
1814 x := Obj.X+Obj.Rect.X;
1815 y := Obj.Y+Obj.Rect.Y;
1816 w := Obj.Rect.Width;
1817 h := Obj.Rect.Height;
1818 end;
1820 procedure TGib.moveBy (dx, dy: Integer); inline;
1821 begin
1822 if (dx <> 0) or (dy <> 0) then
1823 begin
1824 Obj.X += dx;
1825 Obj.Y += dy;
1826 positionChanged();
1827 end;
1828 end;
1831 procedure TShell.getMapBox (out x, y, w, h: Integer); inline;
1832 begin
1833 x := Obj.X;
1834 y := Obj.Y;
1835 w := Obj.Rect.Width;
1836 h := Obj.Rect.Height;
1837 end;
1839 procedure TShell.moveBy (dx, dy: Integer); inline;
1840 begin
1841 if (dx <> 0) or (dy <> 0) then
1842 begin
1843 Obj.X += dx;
1844 Obj.Y += dy;
1845 positionChanged();
1846 end;
1847 end;
1850 procedure TGib.positionChanged (); inline; begin end;
1851 procedure TShell.positionChanged (); inline; begin end;
1854 procedure g_Player_RemoveAllCorpses();
1855 var
1856 i: Integer;
1857 begin
1858 gGibs := nil;
1859 gShells := nil;
1860 SetLength(gGibs, MaxGibs);
1861 SetLength(gShells, MaxGibs);
1862 CurrentGib := 0;
1863 CurrentShell := 0;
1865 if gCorpses <> nil then
1866 for i := 0 to High(gCorpses) do
1867 gCorpses[i].Free();
1869 gCorpses := nil;
1870 SetLength(gCorpses, MaxCorpses);
1871 end;
1873 procedure g_Player_Corpses_SaveState (st: TStream);
1874 var
1875 count, i: Integer;
1876 begin
1877 // Считаем количество существующих трупов
1878 count := 0;
1879 for i := 0 to High(gCorpses) do if (gCorpses[i] <> nil) then Inc(count);
1881 // Количество трупов
1882 utils.writeInt(st, LongInt(count));
1884 if (count = 0) then exit;
1886 // Сохраняем трупы
1887 for i := 0 to High(gCorpses) do
1888 begin
1889 if gCorpses[i] <> nil then
1890 begin
1891 // Название модели
1892 utils.writeStr(st, gCorpses[i].FModel.GetName());
1893 // Тип смерти
1894 utils.writeBool(st, gCorpses[i].Mess);
1895 // Сохраняем данные трупа:
1896 gCorpses[i].SaveState(st);
1897 end;
1898 end;
1899 end;
1902 procedure g_Player_Corpses_LoadState (st: TStream);
1903 var
1904 count, i: Integer;
1905 str: String;
1906 b: Boolean;
1907 begin
1908 assert(st <> nil);
1910 g_Player_RemoveAllCorpses();
1912 // Количество трупов:
1913 count := utils.readLongInt(st);
1914 if (count < 0) or (count > Length(gCorpses)) then raise XStreamError.Create('invalid number of corpses');
1916 if (count = 0) then exit;
1918 // Загружаем трупы
1919 for i := 0 to count-1 do
1920 begin
1921 // Название модели:
1922 str := utils.readStr(st);
1923 // Тип смерти
1924 b := utils.readBool(st);
1925 // Создаем труп
1926 gCorpses[i] := TCorpse.Create(0, 0, str, b);
1927 // Загружаем данные трупа
1928 gCorpses[i].LoadState(st);
1929 end;
1930 end;
1933 { T P l a y e r : }
1935 function TPlayer.isValidViewPort (): Boolean; inline; begin result := (viewPortW > 0) and (viewPortH > 0); end;
1937 procedure TPlayer.BFGHit();
1938 begin
1939 g_Weapon_BFGHit(FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2),
1940 FObj.Y+FObj.Rect.Y+(FObj.Rect.Height div 2));
1941 if g_Game_IsServer and g_Game_IsNet then
1942 MH_SEND_Effect(FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2),
1943 FObj.Y+FObj.Rect.Y+(FObj.Rect.Height div 2),
1944 0, NET_GFX_BFGHIT);
1945 end;
1947 procedure TPlayer.ChangeModel(ModelName: string);
1948 var
1949 locModel: TPlayerModel;
1950 begin
1951 locModel := g_PlayerModel_Get(ModelName);
1952 if locModel = nil then Exit;
1954 FModel.Free();
1955 FModel := locModel;
1956 end;
1958 procedure TPlayer.SetModel(ModelName: string);
1959 var
1960 m: TPlayerModel;
1961 begin
1962 m := g_PlayerModel_Get(ModelName);
1963 if m = nil then
1964 begin
1965 g_SimpleError(Format(_lc[I_GAME_ERROR_MODEL_FALLBACK], [ModelName]));
1966 m := g_PlayerModel_Get('doomer');
1967 if m = nil then
1968 begin
1969 g_FatalError(Format(_lc[I_GAME_ERROR_MODEL], ['doomer']));
1970 Exit;
1971 end;
1972 end;
1974 if FModel <> nil then
1975 FModel.Free();
1977 FModel := m;
1979 if not (gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
1980 FModel.Color := FColor
1981 else
1982 FModel.Color := TEAMCOLOR[FTeam];
1983 FModel.SetWeapon(FCurrWeap);
1984 FModel.SetFlag(FFlag);
1985 SetDirection(FDirection);
1986 end;
1988 procedure TPlayer.SetColor(Color: TRGB);
1989 begin
1990 FColor := Color;
1991 if not (gGameSettings.GameMode in [GM_TDM, GM_CTF]) then
1992 if FModel <> nil then FModel.Color := Color;
1993 end;
1995 function TPlayer.GetColor(): TRGB;
1996 begin
1997 result := FModel.Color;
1998 end;
2000 procedure TPlayer.SwitchTeam;
2001 begin
2002 if g_Game_IsClient then
2003 Exit;
2004 if not (gGameSettings.GameMode in [GM_TDM, GM_CTF]) then Exit;
2006 if gGameOn and FAlive then
2007 Kill(K_SIMPLEKILL, FUID, HIT_SELF);
2009 if FTeam = TEAM_RED then
2010 begin
2011 ChangeTeam(TEAM_BLUE);
2012 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_BLUE], [FName]), True);
2013 if g_Game_IsNet then
2014 MH_SEND_GameEvent(NET_EV_CHANGE_TEAM, TEAM_BLUE, FName);
2015 end
2016 else
2017 begin
2018 ChangeTeam(TEAM_RED);
2019 g_Console_Add(Format(_lc[I_PLAYER_CHTEAM_RED], [FName]), True);
2020 if g_Game_IsNet then
2021 MH_SEND_GameEvent(NET_EV_CHANGE_TEAM, TEAM_RED, FName);
2022 end;
2023 FPreferredTeam := FTeam;
2024 end;
2026 procedure TPlayer.ChangeTeam(Team: Byte);
2027 var
2028 OldTeam: Byte;
2029 begin
2030 OldTeam := FTeam;
2031 FTeam := Team;
2032 case Team of
2033 TEAM_RED, TEAM_BLUE:
2034 FModel.Color := TEAMCOLOR[Team];
2035 else
2036 FModel.Color := FColor;
2037 end;
2038 if (FTeam <> OldTeam) and g_Game_IsNet and g_Game_IsServer then
2039 MH_SEND_PlayerStats(FUID);
2040 end;
2043 procedure TPlayer.CollideItem();
2044 var
2045 i: Integer;
2046 r: Boolean;
2047 begin
2048 if gItems = nil then Exit;
2049 if not FAlive then Exit;
2051 for i := 0 to High(gItems) do
2052 with gItems[i] do
2053 begin
2054 if (ItemType <> ITEM_NONE) and alive then
2055 if g_Obj_Collide(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y, PLAYER_RECT.Width,
2056 PLAYER_RECT.Height, @Obj) then
2057 begin
2058 if not PickItem(ItemType, gItems[i].Respawnable, r) then Continue;
2060 if ItemType in [ITEM_SPHERE_BLUE, ITEM_SPHERE_WHITE, ITEM_INVUL] then
2061 g_Sound_PlayExAt('SOUND_ITEM_GETRULEZ', FObj.X, FObj.Y)
2062 else if ItemType in [ITEM_MEDKIT_SMALL, ITEM_MEDKIT_LARGE, ITEM_MEDKIT_BLACK] then
2063 g_Sound_PlayExAt('SOUND_ITEM_GETMED', FObj.X, FObj.Y)
2064 else g_Sound_PlayExAt('SOUND_ITEM_GETITEM', FObj.X, FObj.Y);
2066 // Надо убрать с карты, если это не ключ, которым нужно поделится с другим игроком:
2067 if r and not ((ItemType in [ITEM_KEY_RED, ITEM_KEY_GREEN, ITEM_KEY_BLUE]) and
2068 (gGameSettings.GameType = GT_SINGLE) and
2069 (g_Player_GetCount() > 1)) then
2070 if not Respawnable then g_Items_Remove(i) else g_Items_Pick(i);
2071 end;
2072 end;
2073 end;
2076 function TPlayer.CollideLevel(XInc, YInc: Integer): Boolean;
2077 begin
2078 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
2079 PLAYER_RECT.Width, PLAYER_RECT.Height, PANEL_WALL,
2080 False);
2081 end;
2083 constructor TPlayer.Create();
2084 begin
2085 viewPortX := 0;
2086 viewPortY := 0;
2087 viewPortW := 0;
2088 viewPortH := 0;
2089 mEDamageType := HIT_SOME;
2091 FIamBot := False;
2092 FDummy := False;
2093 FSpawned := False;
2095 FSawSound := TPlayableSound.Create();
2096 FSawSoundIdle := TPlayableSound.Create();
2097 FSawSoundHit := TPlayableSound.Create();
2098 FSawSoundSelect := TPlayableSound.Create();
2099 FFlameSoundOn := TPlayableSound.Create();
2100 FFlameSoundOff := TPlayableSound.Create();
2101 FFlameSoundWork := TPlayableSound.Create();
2102 FJetSoundFly := TPlayableSound.Create();
2103 FJetSoundOn := TPlayableSound.Create();
2104 FJetSoundOff := TPlayableSound.Create();
2106 FSawSound.SetByName('SOUND_WEAPON_FIRESAW');
2107 FSawSoundIdle.SetByName('SOUND_WEAPON_IDLESAW');
2108 FSawSoundHit.SetByName('SOUND_WEAPON_HITSAW');
2109 FSawSoundSelect.SetByName('SOUND_WEAPON_SELECTSAW');
2110 FFlameSoundOn.SetByName('SOUND_WEAPON_FLAMEON');
2111 FFlameSoundOff.SetByName('SOUND_WEAPON_FLAMEOFF');
2112 FFlameSoundWork.SetByName('SOUND_WEAPON_FLAMEWORK');
2113 FJetSoundFly.SetByName('SOUND_PLAYER_JETFLY');
2114 FJetSoundOn.SetByName('SOUND_PLAYER_JETON');
2115 FJetSoundOff.SetByName('SOUND_PLAYER_JETOFF');
2117 FSpectatePlayer := -1;
2118 FClientID := -1;
2119 FPing := 0;
2120 FLoss := 0;
2121 FSavedStateNum := -1;
2122 FShellTimer := -1;
2123 FFireTime := 0;
2124 FFirePainTime := 0;
2125 FFireAttacker := 0;
2126 FHandicap := 100;
2127 FCorpse := -1;
2129 FActualModelName := 'doomer';
2131 g_Obj_Init(@FObj);
2132 FObj.Rect := PLAYER_RECT;
2134 FBFGFireCounter := -1;
2135 FJustTeleported := False;
2136 FNetTime := 0;
2138 FWaitForFirstSpawn := false;
2139 FPunchAnim := TAnimationState.Create(False, 1, 4);
2140 FPunchAnim.Disable;
2142 resetWeaponQueue();
2143 end;
2145 procedure TPlayer.positionChanged (); inline;
2146 begin
2147 end;
2149 procedure TPlayer.doDamage (v: Integer);
2150 begin
2151 if (v <= 0) then exit;
2152 if (v > 32767) then v := 32767;
2153 Damage(v, 0, 0, 0, mEDamageType);
2154 end;
2156 procedure TPlayer.Damage(value: Word; SpawnerUID: Word; vx, vy: Integer; t: Byte);
2157 var
2158 c: Word;
2159 begin
2160 if (not g_Game_IsClient) and (not FAlive) then
2161 Exit;
2163 FLastHit := t;
2165 // Неуязвимость не спасает от ловушек:
2166 if ((t = HIT_TRAP) or (t = HIT_SELF)) and (not FGodMode) then
2167 begin
2168 if not g_Game_IsClient then
2169 begin
2170 FArmor := 0;
2171 if t = HIT_TRAP then
2172 begin
2173 // Ловушка убивает сразу:
2174 FHealth := -100;
2175 Kill(K_EXTRAHARDKILL, SpawnerUID, t);
2176 end;
2177 if t = HIT_SELF then
2178 begin
2179 // Самоубийство:
2180 FHealth := 0;
2181 Kill(K_SIMPLEKILL, SpawnerUID, t);
2182 end;
2183 end;
2184 // Обнулить действия примочек, чтобы фон пропал
2185 FMegaRulez[MR_SUIT] := 0;
2186 FMegaRulez[MR_INVUL] := 0;
2187 FMegaRulez[MR_INVIS] := 0;
2188 FSpawnInvul := 0;
2189 FBerserk := 0;
2190 end;
2192 // Но от остального спасает:
2193 if FMegaRulez[MR_INVUL] >= gTime then
2194 Exit;
2196 // Чит-код "ГОРЕЦ":
2197 if FGodMode then
2198 Exit;
2200 // Если есть урон своим, или ранил сам себя, или тебя ранил противник:
2201 if LongBool(gGameSettings.Options and GAME_OPTION_TEAMDAMAGE) or
2202 (SpawnerUID = FUID) or
2203 (not SameTeam(FUID, SpawnerUID)) then
2204 begin
2205 FLastSpawnerUID := SpawnerUID;
2207 // Кровь (пузырьки, если в воде):
2208 if gBloodCount > 0 then
2209 begin
2210 c := Min(value, 200)*gBloodCount + Random(Min(value, 200) div 2);
2211 if value div 4 <= c then
2212 c := c - (value div 4)
2213 else
2214 c := 0;
2216 if (t = HIT_SOME) and (vx = 0) and (vy = 0) then
2217 MakeBloodSimple(c)
2218 else
2219 case t of
2220 HIT_TRAP, HIT_ACID, HIT_FLAME, HIT_SELF: MakeBloodSimple(c);
2221 HIT_BFG, HIT_ROCKET, HIT_SOME: MakeBloodVector(c, vx, vy);
2222 end;
2224 {$IFDEF ENABLE_GFX}
2225 if t = HIT_WATER then
2226 begin
2227 g_GFX_Bubbles(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2),
2228 FObj.Y+PLAYER_RECT.Y-4, value div 2, 8, 4);
2229 end;
2230 {$ENDIF}
2231 end;
2233 // Буфер урона:
2234 if FAlive then
2235 Inc(FDamageBuffer, value);
2237 // Вспышка боли:
2238 if gFlash <> 0 then
2239 FPain := FPain + value;
2240 end;
2242 if g_Game_IsServer and g_Game_IsNet then
2243 begin
2244 MH_SEND_PlayerDamage(FUID, t, SpawnerUID, value, vx, vy);
2245 MH_SEND_PlayerStats(FUID);
2246 MH_SEND_PlayerPos(False, FUID);
2247 end;
2248 end;
2250 function TPlayer.Heal(value: Word; Soft: Boolean): Boolean;
2251 begin
2252 Result := False;
2253 if g_Game_IsClient then
2254 Exit;
2255 if not FAlive then
2256 Exit;
2258 if Soft and (FHealth < PLAYER_HP_SOFT) then
2259 begin
2260 IncMax(FHealth, value, PLAYER_HP_SOFT);
2261 Result := True;
2262 end;
2263 if (not Soft) and (FHealth < PLAYER_HP_LIMIT) then
2264 begin
2265 IncMax(FHealth, value, PLAYER_HP_LIMIT);
2266 Result := True;
2267 end;
2269 if Result and g_Game_IsServer and g_Game_IsNet then
2270 MH_SEND_PlayerStats(FUID);
2271 end;
2273 destructor TPlayer.Destroy();
2274 begin
2275 if (gPlayer1 <> nil) and (gPlayer1.FUID = FUID) then
2276 gPlayer1 := nil;
2277 if (gPlayer2 <> nil) and (gPlayer2.FUID = FUID) then
2278 gPlayer2 := nil;
2280 FSawSound.Free();
2281 FSawSoundIdle.Free();
2282 FSawSoundHit.Free();
2283 FSawSoundSelect.Free();
2284 FFlameSoundOn.Free();
2285 FFlameSoundOff.Free();
2286 FFlameSoundWork.Free();
2287 FJetSoundFly.Free();
2288 FJetSoundOn.Free();
2289 FJetSoundOff.Free();
2290 FModel.Free();
2291 FPunchAnim.Free();
2293 inherited;
2294 end;
2296 procedure TPlayer.DoPunch();
2297 begin
2298 FPunchAnim.Reset;
2299 FPunchAnim.Enable;
2300 end;
2302 procedure TPlayer.Fire();
2303 var
2304 f, DidFire: Boolean;
2305 wx, wy, xd, yd: Integer;
2306 locobj: TObj;
2307 begin
2308 if g_Game_IsClient then Exit;
2309 // FBFGFireCounter - время перед выстрелом (для BFG)
2310 // FReloading - время после выстрела (для всего)
2312 if FSpectator then
2313 begin
2314 Respawn(False);
2315 Exit;
2316 end;
2318 if FReloading[FCurrWeap] <> 0 then Exit;
2320 DidFire := False;
2322 f := False;
2323 wx := FObj.X+WEAPONPOINT[FDirection].X;
2324 wy := FObj.Y+WEAPONPOINT[FDirection].Y;
2325 xd := wx+IfThen(FDirection = TDirection.D_LEFT, -30, 30);
2326 yd := wy+firediry();
2328 case FCurrWeap of
2329 WEAPON_KASTET:
2330 begin
2331 DoPunch();
2332 if R_BERSERK in FRulez then
2333 begin
2334 //g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 75, FUID);
2335 locobj.X := FObj.X+FObj.Rect.X;
2336 locobj.Y := FObj.Y+FObj.Rect.Y;
2337 locobj.rect.X := 0;
2338 locobj.rect.Y := 0;
2339 locobj.rect.Width := 39;
2340 locobj.rect.Height := 52;
2341 locobj.Vel.X := (xd-wx) div 2;
2342 locobj.Vel.Y := (yd-wy) div 2;
2343 locobj.Accel.X := xd-wx;
2344 locobj.Accel.y := yd-wy;
2346 if g_Weapon_Hit(@locobj, 50, FUID, HIT_SOME) <> 0 then
2347 g_Sound_PlayExAt('SOUND_WEAPON_HITBERSERK', FObj.X, FObj.Y)
2348 else
2349 g_Sound_PlayExAt('SOUND_WEAPON_MISSBERSERK', FObj.X, FObj.Y);
2351 if (gFlash = 1) and (FPain < 50) then FPain := min(FPain + 25, 50);
2352 end
2353 else
2354 begin
2355 g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 3, FUID);
2356 end;
2358 DidFire := True;
2359 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2360 end;
2362 WEAPON_SAW:
2363 begin
2364 if g_Weapon_chainsaw(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y,
2365 IfThen(gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF], 9, 3), FUID) <> 0 then
2366 begin
2367 FSawSoundSelect.Stop();
2368 FSawSound.Stop();
2369 FSawSoundHit.PlayAt(FObj.X, FObj.Y);
2370 end
2371 else if not FSawSoundHit.IsPlaying() then
2372 begin
2373 FSawSoundSelect.Stop();
2374 FSawSound.PlayAt(FObj.X, FObj.Y);
2375 end;
2377 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2378 DidFire := True;
2379 f := True;
2380 end;
2382 WEAPON_PISTOL:
2383 if FAmmo[A_BULLETS] > 0 then
2384 begin
2385 g_Weapon_pistol(wx, wy, xd, yd, FUID);
2386 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2387 Dec(FAmmo[A_BULLETS]);
2388 FFireAngle := FAngle;
2389 f := True;
2390 DidFire := True;
2391 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
2392 GameVelX, GameVelY-2, SHELL_BULLET);
2393 end;
2395 WEAPON_SHOTGUN1:
2396 if FAmmo[A_SHELLS] > 0 then
2397 begin
2398 g_Weapon_shotgun(wx, wy, xd, yd, FUID);
2399 if not gSoundEffectsDF then g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN', wx, wy);
2400 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2401 Dec(FAmmo[A_SHELLS]);
2402 FFireAngle := FAngle;
2403 f := True;
2404 DidFire := True;
2405 FShellTimer := 10;
2406 FShellType := SHELL_SHELL;
2407 end;
2409 WEAPON_SHOTGUN2:
2410 if FAmmo[A_SHELLS] >= 2 then
2411 begin
2412 g_Weapon_dshotgun(wx, wy, xd, yd, FUID);
2413 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2414 Dec(FAmmo[A_SHELLS], 2);
2415 FFireAngle := FAngle;
2416 f := True;
2417 DidFire := True;
2418 FShellTimer := 13;
2419 FShellType := SHELL_DBLSHELL;
2420 end;
2422 WEAPON_CHAINGUN:
2423 if FAmmo[A_BULLETS] > 0 then
2424 begin
2425 g_Weapon_mgun(wx, wy, xd, yd, FUID);
2426 if not gSoundEffectsDF then g_Sound_PlayExAt('SOUND_WEAPON_FIREPISTOL', wx, wy);
2427 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2428 Dec(FAmmo[A_BULLETS]);
2429 FFireAngle := FAngle;
2430 f := True;
2431 DidFire := True;
2432 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
2433 GameVelX, GameVelY-2, SHELL_BULLET);
2434 end;
2436 WEAPON_ROCKETLAUNCHER:
2437 if FAmmo[A_ROCKETS] > 0 then
2438 begin
2439 g_Weapon_rocket(wx, wy, xd, yd, FUID);
2440 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2441 Dec(FAmmo[A_ROCKETS]);
2442 FFireAngle := FAngle;
2443 f := True;
2444 DidFire := True;
2445 end;
2447 WEAPON_PLASMA:
2448 if FAmmo[A_CELLS] > 0 then
2449 begin
2450 g_Weapon_plasma(wx, wy, xd, yd, FUID);
2451 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2452 Dec(FAmmo[A_CELLS]);
2453 FFireAngle := FAngle;
2454 f := True;
2455 DidFire := True;
2456 end;
2458 WEAPON_BFG:
2459 if (FAmmo[A_CELLS] >= 40) and (FBFGFireCounter = -1) then
2460 begin
2461 FBFGFireCounter := 17;
2462 if not FNoReload then
2463 g_Sound_PlayExAt('SOUND_WEAPON_STARTFIREBFG', FObj.X, FObj.Y);
2464 Dec(FAmmo[A_CELLS], 40);
2465 DidFire := True;
2466 end;
2468 WEAPON_SUPERPULEMET:
2469 if FAmmo[A_SHELLS] > 0 then
2470 begin
2471 g_Weapon_shotgun(wx, wy, xd, yd, FUID);
2472 if not gSoundEffectsDF then g_Sound_PlayExAt('SOUND_WEAPON_FIRECGUN', wx, wy);
2473 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2474 Dec(FAmmo[A_SHELLS]);
2475 FFireAngle := FAngle;
2476 f := True;
2477 DidFire := True;
2478 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
2479 GameVelX, GameVelY-2, SHELL_SHELL);
2480 end;
2482 WEAPON_FLAMETHROWER:
2483 if FAmmo[A_FUEL] > 0 then
2484 begin
2485 g_Weapon_flame(wx, wy, xd, yd, FUID);
2486 FlamerOn;
2487 FReloading[FCurrWeap] := WEAPON_RELOAD[FCurrWeap];
2488 Dec(FAmmo[A_FUEL]);
2489 FFireAngle := FAngle;
2490 f := True;
2491 DidFire := True;
2492 end
2493 else
2494 begin
2495 FlamerOff;
2496 if g_Game_IsNet and g_Game_IsServer then MH_SEND_PlayerStats(FUID);
2497 end;
2498 end;
2500 if g_Game_IsNet then
2501 begin
2502 if DidFire then
2503 begin
2504 if FCurrWeap <> WEAPON_BFG then
2505 MH_SEND_PlayerFire(FUID, FCurrWeap, wx, wy, xd, yd, LastShotID)
2506 else
2507 if not FNoReload then
2508 MH_SEND_Sound(FObj.X, FObj.Y, 'SOUND_WEAPON_STARTFIREBFG');
2509 end;
2511 MH_SEND_PlayerStats(FUID);
2512 end;
2514 if not f then Exit;
2516 if (FAngle = 0) or (FAngle = 180) then SetAction(A_ATTACK)
2517 else if (FAngle = ANGLE_LEFTDOWN) or (FAngle = ANGLE_RIGHTDOWN) then SetAction(A_ATTACKDOWN)
2518 else if (FAngle = ANGLE_LEFTUP) or (FAngle = ANGLE_RIGHTUP) then SetAction(A_ATTACKUP);
2519 end;
2521 function TPlayer.GetAmmoByWeapon(Weapon: Byte): Word;
2522 begin
2523 case Weapon of
2524 WEAPON_PISTOL, WEAPON_CHAINGUN: Result := FAmmo[A_BULLETS];
2525 WEAPON_SHOTGUN1, WEAPON_SHOTGUN2, WEAPON_SUPERPULEMET: Result := FAmmo[A_SHELLS];
2526 WEAPON_ROCKETLAUNCHER: Result := FAmmo[A_ROCKETS];
2527 WEAPON_PLASMA, WEAPON_BFG: Result := FAmmo[A_CELLS];
2528 WEAPON_FLAMETHROWER: Result := FAmmo[A_FUEL];
2529 else Result := 0;
2530 end;
2531 end;
2533 function TPlayer.HeadInLiquid(XInc, YInc: Integer): Boolean;
2534 begin
2535 Result := g_Map_CollidePanel(FObj.X+PLAYER_HEADRECT.X+XInc, FObj.Y+PLAYER_HEADRECT.Y+YInc,
2536 PLAYER_HEADRECT.Width, PLAYER_HEADRECT.Height,
2537 PANEL_WATER or PANEL_ACID1 or PANEL_ACID2, True);
2538 end;
2540 procedure TPlayer.FlamerOn;
2541 begin
2542 FFlameSoundOff.Stop();
2543 FFlameSoundOff.SetPosition(0);
2544 if FFlaming then
2545 begin
2546 if (not FFlameSoundOn.IsPlaying()) and (not FFlameSoundWork.IsPlaying()) then
2547 FFlameSoundWork.PlayAt(FObj.X, FObj.Y);
2548 end
2549 else
2550 begin
2551 FFlameSoundOn.PlayAt(FObj.X, FObj.Y);
2552 FFlaming := True;
2553 end;
2554 end;
2556 procedure TPlayer.FlamerOff;
2557 begin
2558 if FFlaming then
2559 begin
2560 FFlameSoundOn.Stop();
2561 FFlameSoundOn.SetPosition(0);
2562 FFlameSoundWork.Stop();
2563 FFlameSoundWork.SetPosition(0);
2564 FFlameSoundOff.PlayAt(FObj.X, FObj.Y);
2565 FFlaming := False;
2566 end;
2567 end;
2569 procedure TPlayer.JetpackOn;
2570 begin
2571 FJetSoundFly.Stop;
2572 FJetSoundOff.Stop;
2573 FJetSoundOn.SetPosition(0);
2574 FJetSoundOn.PlayAt(FObj.X, FObj.Y);
2575 FlySmoke(8);
2576 end;
2578 procedure TPlayer.JetpackOff;
2579 begin
2580 FJetSoundFly.Stop;
2581 FJetSoundOn.Stop;
2582 FJetSoundOff.SetPosition(0);
2583 FJetSoundOff.PlayAt(FObj.X, FObj.Y);
2584 end;
2586 procedure TPlayer.CatchFire(Attacker: Word; Timeout: Integer = PLAYER_BURN_TIME);
2587 begin
2588 if Timeout <= 0 then
2589 exit;
2590 if (FMegaRulez[MR_SUIT] > gTime) or (FMegaRulez[MR_INVUL] > gTime) then
2591 exit; // Не загораемся когда есть защита
2592 if g_Obj_CollidePanel(@FObj, 0, 0, PANEL_WATER or PANEL_ACID1 or PANEL_ACID2) then
2593 exit; // Не подгораем в воде на всякий случай
2594 if FFireTime <= 0 then
2595 g_Sound_PlayExAt('SOUND_IGNITE', FObj.X, FObj.Y);
2596 FFireTime := Timeout;
2597 FFireAttacker := Attacker;
2598 if g_Game_IsNet and g_Game_IsServer then
2599 MH_SEND_PlayerStats(FUID);
2600 end;
2602 procedure TPlayer.Jump();
2603 begin
2604 if gFly or FJetpack then
2605 begin
2606 // Полет (чит-код или джетпак):
2607 if FObj.Vel.Y > -VEL_FLY then
2608 FObj.Vel.Y := FObj.Vel.Y - 3;
2609 if FJetpack then
2610 begin
2611 if FJetFuel > 0 then
2612 Dec(FJetFuel);
2613 if (FJetFuel < 1) and g_Game_IsServer then
2614 begin
2615 FJetpack := False;
2616 JetpackOff;
2617 if g_Game_IsNet then
2618 MH_SEND_PlayerStats(FUID);
2619 end;
2620 end;
2621 Exit;
2622 end;
2624 // Не включать джетпак в режиме прохождения сквозь стены
2625 if FGhost then
2626 FCanJetpack := False;
2628 // Прыгаем или всплываем:
2629 if (CollideLevel(0, 1) or
2630 g_Map_CollidePanel(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y+36, PLAYER_RECT.Width,
2631 PLAYER_RECT.Height-33, PANEL_STEP, False)
2632 ) and (FObj.Accel.Y = 0) then // Не прыгать, если есть вертикальное ускорение
2633 begin
2634 FObj.Vel.Y := -VEL_JUMP;
2635 FCanJetpack := False;
2636 end
2637 else
2638 begin
2639 if BodyInLiquid(0, 0) then
2640 FObj.Vel.Y := -VEL_SW
2641 else if (FJetFuel > 0) and FCanJetpack and
2642 g_Game_IsServer and (not g_Obj_CollideLiquid(@FObj, 0, 0)) then
2643 begin
2644 FJetpack := True;
2645 JetpackOn;
2646 if g_Game_IsNet then
2647 MH_SEND_PlayerStats(FUID);
2648 end;
2649 end;
2650 end;
2652 procedure TPlayer.Kill(KillType: Byte; SpawnerUID: Word; t: Byte);
2653 var
2654 a, i, k, ab, ar: Byte;
2655 s: String;
2656 mon: TMonster;
2657 plr: TPlayer;
2658 srv, netsrv: Boolean;
2659 DoFrags: Boolean;
2660 OldLR: Byte;
2661 KP: TPlayer;
2662 it: PItem;
2664 procedure PushItem(t: Byte);
2665 var
2666 id: DWORD;
2667 begin
2668 id := g_Items_Create(FObj.X, FObj.Y, t, True, False);
2669 it := g_Items_ByIdx(id);
2670 if KillType = K_EXTRAHARDKILL then // -7..+7; -8..0
2671 begin
2672 g_Obj_Push(@it.Obj, (FObj.Vel.X div 2)-7+Random(15),
2673 (FObj.Vel.Y div 2)-Random(9));
2674 it.positionChanged(); // this updates spatial accelerators
2675 end
2676 else
2677 begin
2678 if KillType = K_HARDKILL then // -5..+5; -5..0
2679 begin
2680 g_Obj_Push(@it.Obj, (FObj.Vel.X div 2)-5+Random(11),
2681 (FObj.Vel.Y div 2)-Random(6));
2682 end
2683 else // -3..+3; -3..0
2684 begin
2685 g_Obj_Push(@it.Obj, (FObj.Vel.X div 2)-3+Random(7),
2686 (FObj.Vel.Y div 2)-Random(4));
2687 end;
2688 it.positionChanged(); // this updates spatial accelerators
2689 end;
2691 if g_Game_IsNet and g_Game_IsServer then
2692 MH_SEND_ItemSpawn(True, id);
2693 end;
2695 begin
2696 DoFrags := (gGameSettings.MaxLives = 0) or (gGameSettings.GameMode = GM_COOP);
2697 Srv := g_Game_IsServer;
2698 Netsrv := g_Game_IsServer and g_Game_IsNet;
2699 if Srv then FDeath := FDeath + 1;
2700 if FAlive then
2701 begin
2702 if FGhost then
2703 FGhost := False;
2704 if not FPhysics then
2705 FPhysics := True;
2706 FAlive := False;
2707 end;
2708 FShellTimer := -1;
2710 if (gGameSettings.MaxLives > 0) and Srv and (gLMSRespawn = LMS_RESPAWN_NONE) then
2711 begin
2712 if FLives > 0 then FLives := FLives - 1;
2713 if FLives = 0 then FNoRespawn := True;
2714 end;
2716 // Номер типа смерти:
2717 a := 1;
2718 case KillType of
2719 K_SIMPLEKILL: a := 1;
2720 K_HARDKILL: a := 2;
2721 K_EXTRAHARDKILL: a := 3;
2722 K_FALLKILL: a := 4;
2723 end;
2725 // Звук смерти:
2726 if not FModel.PlaySound(MODELSOUND_DIE, a, FObj.X, FObj.Y) then
2727 for i := 1 to 3 do
2728 if FModel.PlaySound(MODELSOUND_DIE, i, FObj.X, FObj.Y) then
2729 Break;
2731 // Время респауна:
2732 if Srv then
2733 case KillType of
2734 K_SIMPLEKILL:
2735 FTime[T_RESPAWN] := gTime + TIME_RESPAWN1;
2736 K_HARDKILL:
2737 FTime[T_RESPAWN] := gTime + TIME_RESPAWN2;
2738 K_EXTRAHARDKILL, K_FALLKILL:
2739 FTime[T_RESPAWN] := gTime + TIME_RESPAWN3;
2740 end;
2742 // Переключаем состояние:
2743 case KillType of
2744 K_SIMPLEKILL:
2745 SetAction(A_DIE1);
2746 K_HARDKILL, K_EXTRAHARDKILL:
2747 SetAction(A_DIE2);
2748 end;
2750 // Реакция монстров на смерть игрока:
2751 if (KillType <> K_FALLKILL) and (Srv) then
2752 g_Monsters_killedp();
2754 if SpawnerUID = FUID then
2755 begin // Самоубился
2756 if Srv and (DoFrags or (gGameSettings.GameMode = GM_TDM)) then
2757 begin
2758 Dec(FFrags);
2759 FLastFrag := 0;
2760 end;
2761 g_Console_Add(Format(_lc[I_PLAYER_KILL_SELF], [FName]), True);
2762 end
2763 else
2764 if g_GetUIDType(SpawnerUID) = UID_PLAYER then
2765 begin // Убит другим игроком
2766 KP := g_Player_Get(SpawnerUID);
2767 if (KP <> nil) and Srv then
2768 begin
2769 if (DoFrags or (gGameSettings.GameMode = GM_TDM)) then
2770 if SameTeam(FUID, SpawnerUID) then
2771 begin
2772 Dec(KP.FFrags);
2773 KP.FLastFrag := 0;
2774 end else
2775 begin
2776 Inc(KP.FFrags);
2777 KP.FragCombo();
2778 end;
2780 if (gGameSettings.GameMode = GM_TDM) and DoFrags then
2781 Inc(gTeamStat[KP.Team].Goals,
2782 IfThen(SameTeam(FUID, SpawnerUID), -1, 1));
2784 if netsrv then MH_SEND_PlayerStats(SpawnerUID);
2785 end;
2787 plr := g_Player_Get(SpawnerUID);
2788 if plr = nil then
2789 s := '?'
2790 else
2791 s := plr.FName;
2793 case KillType of
2794 K_HARDKILL:
2795 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_2],
2796 [FName, s]),
2797 gShowKillMsg);
2798 K_EXTRAHARDKILL:
2799 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_1],
2800 [FName, s]),
2801 gShowKillMsg);
2802 else
2803 g_Console_Add(Format(_lc[I_PLAYER_KILL],
2804 [FName, s]),
2805 gShowKillMsg);
2806 end;
2807 end
2808 else if g_GetUIDType(SpawnerUID) = UID_MONSTER then
2809 begin // Убит монстром
2810 mon := g_Monsters_ByUID(SpawnerUID);
2811 if mon = nil then
2812 s := '?'
2813 else
2814 s := g_Mons_GetKilledByTypeId(mon.MonsterType);
2816 case KillType of
2817 K_HARDKILL:
2818 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_2],
2819 [FName, s]),
2820 gShowKillMsg);
2821 K_EXTRAHARDKILL:
2822 g_Console_Add(Format(_lc[I_PLAYER_KILL_EXTRAHARD_1],
2823 [FName, s]),
2824 gShowKillMsg);
2825 else
2826 g_Console_Add(Format(_lc[I_PLAYER_KILL],
2827 [FName, s]),
2828 gShowKillMsg);
2829 end;
2830 end
2831 else // Особые типы смерти
2832 case t of
2833 HIT_DISCON: ;
2834 HIT_SELF: g_Console_Add(Format(_lc[I_PLAYER_KILL_SELF], [FName]), True);
2835 HIT_FALL: g_Console_Add(Format(_lc[I_PLAYER_KILL_FALL], [FName]), True);
2836 HIT_WATER: g_Console_Add(Format(_lc[I_PLAYER_KILL_WATER], [FName]), True);
2837 HIT_ACID: g_Console_Add(Format(_lc[I_PLAYER_KILL_ACID], [FName]), True);
2838 HIT_TRAP: g_Console_Add(Format(_lc[I_PLAYER_KILL_TRAP], [FName]), True);
2839 else g_Console_Add(Format(_lc[I_PLAYER_DIED], [FName]), True);
2840 end;
2842 if Srv then
2843 begin
2844 // Выброс оружия:
2845 for a := WP_FIRST to WP_LAST do
2846 if FWeapon[a] then
2847 begin
2848 case a of
2849 WEAPON_SAW: i := ITEM_WEAPON_SAW;
2850 WEAPON_SHOTGUN1: i := ITEM_WEAPON_SHOTGUN1;
2851 WEAPON_SHOTGUN2: i := ITEM_WEAPON_SHOTGUN2;
2852 WEAPON_CHAINGUN: i := ITEM_WEAPON_CHAINGUN;
2853 WEAPON_ROCKETLAUNCHER: i := ITEM_WEAPON_ROCKETLAUNCHER;
2854 WEAPON_PLASMA: i := ITEM_WEAPON_PLASMA;
2855 WEAPON_BFG: i := ITEM_WEAPON_BFG;
2856 WEAPON_SUPERPULEMET: i := ITEM_WEAPON_SUPERPULEMET;
2857 WEAPON_FLAMETHROWER: i := ITEM_WEAPON_FLAMETHROWER;
2858 else i := 0;
2859 end;
2861 if i <> 0 then
2862 PushItem(i);
2863 end;
2865 // Выброс рюкзака:
2866 if R_ITEM_BACKPACK in FRulez then
2867 PushItem(ITEM_AMMO_BACKPACK);
2869 // Выброс ракетного ранца:
2870 if FJetFuel > 0 then
2871 PushItem(ITEM_JETPACK);
2873 // Выброс ключей:
2874 if (not (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) or
2875 (not LongBool(gGameSettings.Options and GAME_OPTION_DMKEYS)) then
2876 begin
2877 if R_KEY_RED in FRulez then
2878 PushItem(ITEM_KEY_RED);
2880 if R_KEY_GREEN in FRulez then
2881 PushItem(ITEM_KEY_GREEN);
2883 if R_KEY_BLUE in FRulez then
2884 PushItem(ITEM_KEY_BLUE);
2885 end;
2887 // Выброс флага:
2888 DropFlag(KillType = K_FALLKILL);
2889 end;
2891 FCorpse := g_Player_CreateCorpse(Self);
2893 if Srv and (gGameSettings.MaxLives > 0) and FNoRespawn and
2894 (gLMSRespawn = LMS_RESPAWN_NONE) then
2895 begin
2896 a := 0;
2897 k := 0;
2898 ar := 0;
2899 ab := 0;
2900 for i := Low(gPlayers) to High(gPlayers) do
2901 begin
2902 if gPlayers[i] = nil then continue;
2903 if (not gPlayers[i].FNoRespawn) and (not gPlayers[i].FSpectator) then
2904 begin
2905 Inc(a);
2906 if gPlayers[i].FTeam = TEAM_RED then Inc(ar)
2907 else if gPlayers[i].FTeam = TEAM_BLUE then Inc(ab);
2908 k := i;
2909 end;
2910 end;
2912 OldLR := gLMSRespawn;
2913 if (gGameSettings.GameMode = GM_COOP) then
2914 begin
2915 if (a = 0) then
2916 begin
2917 // everyone is dead, restart the map
2918 g_Game_Message(_lc[I_MESSAGE_LMS_LOSE], 144);
2919 if Netsrv then
2920 MH_SEND_GameEvent(NET_EV_LMS_LOSE);
2921 gLMSRespawn := LMS_RESPAWN_FINAL;
2922 gLMSRespawnTime := gTime + 5000;
2923 end
2924 else if (a = 1) then
2925 begin
2926 if (gPlayers[k] <> nil) and not (gPlayers[k] is TBot) then
2927 if (gPlayers[k] = gPlayer1) or
2928 (gPlayers[k] = gPlayer2) then
2929 g_Console_Add('*** ' + _lc[I_MESSAGE_LMS_SURVIVOR] + ' ***', True)
2930 else if Netsrv and (gPlayers[k].FClientID >= 0) then
2931 MH_SEND_GameEvent(NET_EV_LMS_SURVIVOR, 0, 'N', gPlayers[k].FClientID);
2932 end;
2933 end
2934 else if (gGameSettings.GameMode = GM_TDM) then
2935 begin
2936 if (ab = 0) and (ar <> 0) then
2937 begin
2938 // blu team ded
2939 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_RED])]), 144);
2940 if Netsrv then
2941 MH_SEND_GameEvent(NET_EV_TLMS_WIN, TEAM_RED);
2942 Inc(gTeamStat[TEAM_RED].Goals);
2943 gLMSRespawn := LMS_RESPAWN_FINAL;
2944 gLMSRespawnTime := gTime + 5000;
2945 end
2946 else if (ar = 0) and (ab <> 0) then
2947 begin
2948 // red team ded
2949 g_Game_Message(Format(_lc[I_MESSAGE_TLMS_WIN], [AnsiUpperCase(_lc[I_GAME_TEAM_BLUE])]), 144);
2950 if Netsrv then
2951 MH_SEND_GameEvent(NET_EV_TLMS_WIN, TEAM_BLUE);
2952 Inc(gTeamStat[TEAM_BLUE].Goals);
2953 gLMSRespawn := LMS_RESPAWN_FINAL;
2954 gLMSRespawnTime := gTime + 5000;
2955 end
2956 else if (ar = 0) and (ab = 0) then
2957 begin
2958 // everyone ded
2959 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
2960 if Netsrv then
2961 MH_SEND_GameEvent(NET_EV_LMS_DRAW, 0, FName);
2962 gLMSRespawn := LMS_RESPAWN_FINAL;
2963 gLMSRespawnTime := gTime + 5000;
2964 end;
2965 end
2966 else if (gGameSettings.GameMode = GM_DM) then
2967 begin
2968 if (a = 1) then
2969 begin
2970 if gPlayers[k] <> nil then
2971 with gPlayers[k] do
2972 begin
2973 // survivor is the winner
2974 g_Game_Message(Format(_lc[I_MESSAGE_LMS_WIN], [AnsiUpperCase(FName)]), 144);
2975 if Netsrv then
2976 MH_SEND_GameEvent(NET_EV_LMS_WIN, 0, FName);
2977 Inc(FFrags);
2978 end;
2979 gLMSRespawn := LMS_RESPAWN_FINAL;
2980 gLMSRespawnTime := gTime + 5000;
2981 end
2982 else if (a = 0) then
2983 begin
2984 // everyone is dead, restart the map
2985 g_Game_Message(_lc[I_GAME_WIN_DRAW], 144);
2986 if Netsrv then
2987 MH_SEND_GameEvent(NET_EV_LMS_DRAW, 0, FName);
2988 gLMSRespawn := LMS_RESPAWN_FINAL;
2989 gLMSRespawnTime := gTime + 5000;
2990 end;
2991 end;
2992 if srv and (OldLR = LMS_RESPAWN_NONE) and (gLMSRespawn > LMS_RESPAWN_NONE) then
2993 begin
2994 if NetMode = NET_SERVER then
2995 MH_SEND_GameEvent(NET_EV_LMS_WARMUP, gLMSRespawnTime - gTime)
2996 else
2997 g_Console_Add(Format(_lc[I_MSG_WARMUP_START], [(gLMSRespawnTime - gTime) div 1000]), True);
2998 end;
2999 end;
3001 if Netsrv then
3002 begin
3003 MH_SEND_PlayerStats(FUID);
3004 MH_SEND_PlayerDeath(FUID, KillType, t, SpawnerUID);
3005 if gGameSettings.GameMode = GM_TDM then MH_SEND_GameStats;
3006 end;
3008 if srv and FNoRespawn then Spectate(True);
3009 FWantsInGame := True;
3010 end;
3012 function TPlayer.BodyInLiquid(XInc, YInc: Integer): Boolean;
3013 begin
3014 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc, PLAYER_RECT.Width,
3015 PLAYER_RECT.Height-20, PANEL_WATER or PANEL_ACID1 or PANEL_ACID2, False);
3016 end;
3018 function TPlayer.BodyInAcid(XInc, YInc: Integer): Boolean;
3019 begin
3020 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc, PLAYER_RECT.Width,
3021 PLAYER_RECT.Height-20, PANEL_ACID1 or PANEL_ACID2, False);
3022 end;
3024 procedure TPlayer.MakeBloodSimple(Count: Word);
3025 {$IFDEF ENABLE_GFX}
3026 var Blood: TModelBlood;
3027 {$ENDIF}
3028 begin
3029 {$IFDEF ENABLE_GFX}
3030 Blood := SELF.FModel.GetBlood();
3031 g_GFX_Blood(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)+8,
3032 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2),
3033 Count div 2, 3, -1, 16, (PLAYER_RECT.Height*2 div 3),
3034 Blood.R, Blood.G, Blood.B, Blood.Kind);
3035 g_GFX_Blood(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-8,
3036 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2),
3037 Count div 2, -3, -1, 16, (PLAYER_RECT.Height*2) div 3,
3038 Blood.R, Blood.G, Blood.B, Blood.Kind);
3039 {$ENDIF}
3040 end;
3042 procedure TPlayer.MakeBloodVector(Count: Word; VelX, VelY: Integer);
3043 {$IFDEF ENABLE_GFX}
3044 var Blood: TModelBlood;
3045 {$ENDIF}
3046 begin
3047 {$IFDEF ENABLE_GFX}
3048 Blood := SELF.FModel.GetBlood();
3049 g_GFX_Blood(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2),
3050 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2),
3051 Count, VelX, VelY, 16, (PLAYER_RECT.Height*2) div 3,
3052 Blood.R, Blood.G, Blood.B, Blood.Kind);
3053 {$ENDIF}
3054 end;
3056 procedure TPlayer.QueueWeaponSwitch(Weapon: Byte);
3057 begin
3058 if g_Game_IsClient then Exit;
3059 if Weapon > High(FWeapon) then Exit;
3060 FNextWeap := FNextWeap or (1 shl Weapon);
3061 end;
3063 procedure TPlayer.resetWeaponQueue ();
3064 begin
3065 FNextWeap := 0;
3066 FNextWeapDelay := 0;
3067 end;
3069 function TPlayer.hasAmmoForWeapon (weapon: Byte): Boolean;
3070 begin
3071 result := false;
3072 case weapon of
3073 WEAPON_KASTET, WEAPON_SAW: result := true;
3074 WEAPON_SHOTGUN1, WEAPON_SHOTGUN2, WEAPON_SUPERPULEMET: result := (FAmmo[A_SHELLS] > 0);
3075 WEAPON_PISTOL, WEAPON_CHAINGUN: result := (FAmmo[A_BULLETS] > 0);
3076 WEAPON_ROCKETLAUNCHER: result := (FAmmo[A_ROCKETS] > 0);
3077 WEAPON_PLASMA, WEAPON_BFG: result := (FAmmo[A_CELLS] > 0);
3078 WEAPON_FLAMETHROWER: result := (FAmmo[A_FUEL] > 0);
3079 else result := (weapon < length(FWeapon));
3080 end;
3081 end;
3083 // return 255 for "no switch"
3084 function TPlayer.getNextWeaponIndex (): Byte;
3085 var
3086 i: Word;
3087 wantThisWeapon: array[0..64] of Boolean;
3088 wwc: Integer = 0; //HACK!
3089 dir, cwi: Integer;
3090 begin
3091 result := 255; // default result: "no switch"
3092 // had weapon cycling on previous frame? remove that flag
3093 if (FNextWeap and $2000) <> 0 then
3094 begin
3095 FNextWeap := FNextWeap and $1FFF;
3096 FNextWeapDelay := 0;
3097 end;
3098 // cycling has priority
3099 if (FNextWeap and $C000) <> 0 then
3100 begin
3101 if (FNextWeap and $8000) <> 0 then
3102 dir := 1
3103 else
3104 dir := -1;
3105 FNextWeap := FNextWeap or $2000; // we need this
3106 if FNextWeapDelay > 0 then
3107 exit; // cooldown time
3108 cwi := FCurrWeap;
3109 for i := 0 to High(FWeapon) do
3110 begin
3111 cwi := (cwi+length(FWeapon)+dir) mod length(FWeapon);
3112 if FWeapon[cwi] then
3113 begin
3114 //e_WriteLog(Format(' SWITCH: cur=%d; new=%d', [FCurrWeap, cwi]), MSG_WARNING);
3115 result := Byte(cwi);
3116 FNextWeapDelay := WEAPON_DELAY;
3117 exit;
3118 end;
3119 end;
3120 resetWeaponQueue();
3121 exit;
3122 end;
3123 // no cycling
3124 for i := 0 to High(wantThisWeapon) do
3125 wantThisWeapon[i] := false;
3126 for i := 0 to High(FWeapon) do
3127 if (FNextWeap and (1 shl i)) <> 0 then
3128 begin
3129 wantThisWeapon[i] := true;
3130 Inc(wwc);
3131 end;
3132 // exclude currently selected weapon from the set
3133 wantThisWeapon[FCurrWeap] := false;
3134 // slow down alterations a little
3135 if wwc > 1 then
3136 begin
3137 //e_WriteLog(Format(' FNextWeap=%x; delay=%d', [FNextWeap, FNextWeapDelay]), MSG_WARNING);
3138 // more than one weapon requested, assume "alteration" and check alteration delay
3139 if FNextWeapDelay > 0 then
3140 begin
3141 FNextWeap := 0;
3142 exit;
3143 end; // yeah
3144 end;
3145 // do not reset weapon queue, it will be done in `RealizeCurrentWeapon()`
3146 // but clear all counters if no weapon should be switched
3147 if wwc < 1 then
3148 begin
3149 resetWeaponQueue();
3150 exit;
3151 end;
3152 //e_WriteLog(Format('wwc=%d', [wwc]), MSG_WARNING);
3153 // try weapons in descending order
3154 for i := High(FWeapon) downto 0 do
3155 begin
3156 if wantThisWeapon[i] and FWeapon[i] and ((wwc = 1) or hasAmmoForWeapon(i)) then
3157 begin
3158 // i found her!
3159 result := Byte(i);
3160 resetWeaponQueue();
3161 FNextWeapDelay := WEAPON_DELAY * 2; // anyway, 'cause why not
3162 exit;
3163 end;
3164 end;
3165 // no suitable weapon found, so reset the queue, to avoid accidental "queuing" of weapon w/o ammo
3166 resetWeaponQueue();
3167 end;
3169 procedure TPlayer.RealizeCurrentWeapon();
3170 function switchAllowed (): Boolean;
3171 var
3172 i: Byte;
3173 begin
3174 result := false;
3175 if FBFGFireCounter <> -1 then
3176 exit;
3177 if FTime[T_SWITCH] > gTime then
3178 exit;
3179 for i := WP_FIRST to WP_LAST do
3180 if FReloading[i] > 0 then
3181 exit;
3182 result := true;
3183 end;
3185 var
3186 nw: Byte;
3187 begin
3188 //e_WriteLog(Format('***RealizeCurrentWeapon: FNextWeap=%x; FNextWeapDelay=%d', [FNextWeap, FNextWeapDelay]), MSG_WARNING);
3189 //FNextWeap := FNextWeap and $1FFF;
3190 if FNextWeapDelay > 0 then Dec(FNextWeapDelay); // "alteration delay"
3192 if not switchAllowed then
3193 begin
3194 //HACK for weapon cycling
3195 if (FNextWeap and $E000) <> 0 then FNextWeap := 0;
3196 exit;
3197 end;
3199 nw := getNextWeaponIndex();
3200 if nw = 255 then exit; // don't reset anything here
3201 if nw > High(FWeapon) then
3202 begin
3203 // don't forget to reset queue here!
3204 //e_WriteLog(' RealizeCurrentWeapon: WUTAFUUUU', MSG_WARNING);
3205 resetWeaponQueue();
3206 exit;
3207 end;
3209 if FWeapon[nw] then
3210 begin
3211 FCurrWeap := nw;
3212 FTime[T_SWITCH] := gTime+156;
3213 if FCurrWeap = WEAPON_SAW then FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
3214 FModel.SetWeapon(FCurrWeap);
3215 if g_Game_IsNet then MH_SEND_PlayerStats(FUID);
3216 end;
3217 end;
3219 procedure TPlayer.NextWeapon();
3220 begin
3221 if g_Game_IsClient then Exit;
3222 FNextWeap := $8000;
3223 end;
3225 procedure TPlayer.PrevWeapon();
3226 begin
3227 if g_Game_IsClient then Exit;
3228 FNextWeap := $4000;
3229 end;
3231 procedure TPlayer.SetWeapon(W: Byte);
3232 begin
3233 if FCurrWeap <> W then
3234 if W = WEAPON_SAW then
3235 FSawSoundSelect.PlayAt(FObj.X, FObj.Y);
3237 FCurrWeap := W;
3238 FModel.SetWeapon(CurrWeap);
3239 resetWeaponQueue();
3240 end;
3242 function TPlayer.PickItem(ItemType: Byte; arespawn: Boolean; var remove: Boolean): Boolean;
3244 function allowBerserkSwitching (): Boolean;
3245 begin
3246 if (FBFGFireCounter <> -1) then begin result := false; exit; end;
3247 result := true;
3248 if gBerserkAutoswitch then exit;
3249 if not conIsCheatsEnabled then exit;
3250 result := false;
3251 end;
3253 var
3254 a: Boolean;
3255 begin
3256 Result := False;
3257 if g_Game_IsClient then Exit;
3259 // a = true - место спавна предмета:
3260 a := LongBool(gGameSettings.Options and GAME_OPTION_WEAPONSTAY) and arespawn;
3261 remove := not a;
3263 case ItemType of
3264 ITEM_MEDKIT_SMALL:
3265 if (FHealth < PLAYER_HP_SOFT) or (FFireTime > 0) then
3266 begin
3267 if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 10, PLAYER_HP_SOFT);
3268 Result := True;
3269 remove := True;
3270 FFireTime := 0;
3271 if gFlash = 2 then Inc(FPickup, 5);
3272 end;
3274 ITEM_MEDKIT_LARGE:
3275 if (FHealth < PLAYER_HP_SOFT) or (FFireTime > 0) then
3276 begin
3277 if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 25, PLAYER_HP_SOFT);
3278 Result := True;
3279 remove := True;
3280 FFireTime := 0;
3281 if gFlash = 2 then Inc(FPickup, 5);
3282 end;
3284 ITEM_ARMOR_GREEN:
3285 if FArmor < PLAYER_AP_SOFT then
3286 begin
3287 FArmor := PLAYER_AP_SOFT;
3288 Result := True;
3289 remove := True;
3290 if gFlash = 2 then Inc(FPickup, 5);
3291 end;
3293 ITEM_ARMOR_BLUE:
3294 if FArmor < PLAYER_AP_LIMIT then
3295 begin
3296 FArmor := PLAYER_AP_LIMIT;
3297 Result := True;
3298 remove := True;
3299 if gFlash = 2 then Inc(FPickup, 5);
3300 end;
3302 ITEM_SPHERE_BLUE:
3303 if (FHealth < PLAYER_HP_LIMIT) or (FFireTime > 0) then
3304 begin
3305 if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 100, PLAYER_HP_LIMIT);
3306 Result := True;
3307 remove := True;
3308 FFireTime := 0;
3309 if gFlash = 2 then Inc(FPickup, 5);
3310 end;
3312 ITEM_SPHERE_WHITE:
3313 if (FHealth < PLAYER_HP_LIMIT) or (FArmor < PLAYER_AP_LIMIT) or (FFireTime > 0) then
3314 begin
3315 if FHealth < PLAYER_HP_LIMIT then
3316 FHealth := PLAYER_HP_LIMIT;
3317 if FArmor < PLAYER_AP_LIMIT then
3318 FArmor := PLAYER_AP_LIMIT;
3319 Result := True;
3320 remove := True;
3321 FFireTime := 0;
3322 if gFlash = 2 then Inc(FPickup, 5);
3323 end;
3325 ITEM_WEAPON_SAW:
3326 if (not FWeapon[WEAPON_SAW]) or ((not arespawn) and (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF])) then
3327 begin
3328 FWeapon[WEAPON_SAW] := True;
3329 Result := True;
3330 if gFlash = 2 then Inc(FPickup, 5);
3331 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3332 end;
3334 ITEM_WEAPON_SHOTGUN1:
3335 if (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or not FWeapon[WEAPON_SHOTGUN1] then
3336 begin
3337 // Нужно, чтобы не взять все пули сразу:
3338 if a and FWeapon[WEAPON_SHOTGUN1] then Exit;
3340 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3341 FWeapon[WEAPON_SHOTGUN1] := True;
3342 Result := True;
3343 if gFlash = 2 then Inc(FPickup, 5);
3344 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3345 end;
3347 ITEM_WEAPON_SHOTGUN2:
3348 if (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or not FWeapon[WEAPON_SHOTGUN2] then
3349 begin
3350 if a and FWeapon[WEAPON_SHOTGUN2] then Exit;
3352 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3353 FWeapon[WEAPON_SHOTGUN2] := True;
3354 Result := True;
3355 if gFlash = 2 then Inc(FPickup, 5);
3356 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3357 end;
3359 ITEM_WEAPON_CHAINGUN:
3360 if (FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS]) or not FWeapon[WEAPON_CHAINGUN] then
3361 begin
3362 if a and FWeapon[WEAPON_CHAINGUN] then Exit;
3364 IncMax(FAmmo[A_BULLETS], 50, FMaxAmmo[A_BULLETS]);
3365 FWeapon[WEAPON_CHAINGUN] := True;
3366 Result := True;
3367 if gFlash = 2 then Inc(FPickup, 5);
3368 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3369 end;
3371 ITEM_WEAPON_ROCKETLAUNCHER:
3372 if (FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS]) or not FWeapon[WEAPON_ROCKETLAUNCHER] then
3373 begin
3374 if a and FWeapon[WEAPON_ROCKETLAUNCHER] then Exit;
3376 IncMax(FAmmo[A_ROCKETS], 2, FMaxAmmo[A_ROCKETS]);
3377 FWeapon[WEAPON_ROCKETLAUNCHER] := True;
3378 Result := True;
3379 if gFlash = 2 then Inc(FPickup, 5);
3380 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3381 end;
3383 ITEM_WEAPON_PLASMA:
3384 if (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or not FWeapon[WEAPON_PLASMA] then
3385 begin
3386 if a and FWeapon[WEAPON_PLASMA] then Exit;
3388 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3389 FWeapon[WEAPON_PLASMA] := True;
3390 Result := True;
3391 if gFlash = 2 then Inc(FPickup, 5);
3392 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3393 end;
3395 ITEM_WEAPON_BFG:
3396 if (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or not FWeapon[WEAPON_BFG] then
3397 begin
3398 if a and FWeapon[WEAPON_BFG] then Exit;
3400 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3401 FWeapon[WEAPON_BFG] := True;
3402 Result := True;
3403 if gFlash = 2 then Inc(FPickup, 5);
3404 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3405 end;
3407 ITEM_WEAPON_SUPERPULEMET:
3408 if (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or not FWeapon[WEAPON_SUPERPULEMET] then
3409 begin
3410 if a and FWeapon[WEAPON_SUPERPULEMET] then Exit;
3412 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3413 FWeapon[WEAPON_SUPERPULEMET] := True;
3414 Result := True;
3415 if gFlash = 2 then Inc(FPickup, 5);
3416 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3417 end;
3419 ITEM_WEAPON_FLAMETHROWER:
3420 if (FAmmo[A_FUEL] < FMaxAmmo[A_FUEL]) or not FWeapon[WEAPON_FLAMETHROWER] then
3421 begin
3422 if a and FWeapon[WEAPON_FLAMETHROWER] then Exit;
3424 IncMax(FAmmo[A_FUEL], 100, FMaxAmmo[A_FUEL]);
3425 FWeapon[WEAPON_FLAMETHROWER] := True;
3426 Result := True;
3427 if gFlash = 2 then Inc(FPickup, 5);
3428 if a and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETWEAPON');
3429 end;
3431 ITEM_AMMO_BULLETS:
3432 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then
3433 begin
3434 IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
3435 Result := True;
3436 remove := True;
3437 if gFlash = 2 then Inc(FPickup, 5);
3438 end;
3440 ITEM_AMMO_BULLETS_BOX:
3441 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then
3442 begin
3443 IncMax(FAmmo[A_BULLETS], 50, FMaxAmmo[A_BULLETS]);
3444 Result := True;
3445 remove := True;
3446 if gFlash = 2 then Inc(FPickup, 5);
3447 end;
3449 ITEM_AMMO_SHELLS:
3450 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then
3451 begin
3452 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3453 Result := True;
3454 remove := True;
3455 if gFlash = 2 then Inc(FPickup, 5);
3456 end;
3458 ITEM_AMMO_SHELLS_BOX:
3459 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then
3460 begin
3461 IncMax(FAmmo[A_SHELLS], 25, FMaxAmmo[A_SHELLS]);
3462 Result := True;
3463 remove := True;
3464 if gFlash = 2 then Inc(FPickup, 5);
3465 end;
3467 ITEM_AMMO_ROCKET:
3468 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then
3469 begin
3470 IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
3471 Result := True;
3472 remove := True;
3473 if gFlash = 2 then Inc(FPickup, 5);
3474 end;
3476 ITEM_AMMO_ROCKET_BOX:
3477 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then
3478 begin
3479 IncMax(FAmmo[A_ROCKETS], 5, FMaxAmmo[A_ROCKETS]);
3480 Result := True;
3481 remove := True;
3482 if gFlash = 2 then Inc(FPickup, 5);
3483 end;
3485 ITEM_AMMO_CELL:
3486 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then
3487 begin
3488 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3489 Result := True;
3490 remove := True;
3491 if gFlash = 2 then Inc(FPickup, 5);
3492 end;
3494 ITEM_AMMO_CELL_BIG:
3495 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then
3496 begin
3497 IncMax(FAmmo[A_CELLS], 100, FMaxAmmo[A_CELLS]);
3498 Result := True;
3499 remove := True;
3500 if gFlash = 2 then Inc(FPickup, 5);
3501 end;
3503 ITEM_AMMO_FUELCAN:
3504 if FAmmo[A_FUEL] < FMaxAmmo[A_FUEL] then
3505 begin
3506 IncMax(FAmmo[A_FUEL], 100, FMaxAmmo[A_FUEL]);
3507 Result := True;
3508 remove := True;
3509 if gFlash = 2 then Inc(FPickup, 5);
3510 end;
3512 ITEM_AMMO_BACKPACK:
3513 if not(R_ITEM_BACKPACK in FRulez) or
3514 (FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS]) or
3515 (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or
3516 (FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS]) or
3517 (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or
3518 (FAmmo[A_FUEL] < FMaxAmmo[A_FUEL]) then
3519 begin
3520 FMaxAmmo[A_BULLETS] := AmmoLimits[1, A_BULLETS];
3521 FMaxAmmo[A_SHELLS] := AmmoLimits[1, A_SHELLS];
3522 FMaxAmmo[A_ROCKETS] := AmmoLimits[1, A_ROCKETS];
3523 FMaxAmmo[A_CELLS] := AmmoLimits[1, A_CELLS];
3524 FMaxAmmo[A_FUEL] := AmmoLimits[1, A_FUEL];
3526 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then
3527 IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
3528 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then
3529 IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
3530 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then
3531 IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
3532 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then
3533 IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
3534 if FAmmo[A_FUEL] < FMaxAmmo[A_FUEL] then
3535 IncMax(FAmmo[A_FUEL], 50, FMaxAmmo[A_FUEL]);
3537 FRulez := FRulez + [R_ITEM_BACKPACK];
3538 Result := True;
3539 remove := True;
3540 if gFlash = 2 then Inc(FPickup, 5);
3541 end;
3543 ITEM_KEY_RED:
3544 if not(R_KEY_RED in FRulez) then
3545 begin
3546 Include(FRulez, R_KEY_RED);
3547 Result := True;
3548 remove := (gGameSettings.GameMode <> GM_COOP) and (g_Player_GetCount() < 2);
3549 if gFlash = 2 then Inc(FPickup, 5);
3550 if (not remove) and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETITEM');
3551 end;
3553 ITEM_KEY_GREEN:
3554 if not(R_KEY_GREEN in FRulez) then
3555 begin
3556 Include(FRulez, R_KEY_GREEN);
3557 Result := True;
3558 remove := (gGameSettings.GameMode <> GM_COOP) and (g_Player_GetCount() < 2);
3559 if gFlash = 2 then Inc(FPickup, 5);
3560 if (not remove) and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETITEM');
3561 end;
3563 ITEM_KEY_BLUE:
3564 if not(R_KEY_BLUE in FRulez) then
3565 begin
3566 Include(FRulez, R_KEY_BLUE);
3567 Result := True;
3568 remove := (gGameSettings.GameMode <> GM_COOP) and (g_Player_GetCount() < 2);
3569 if gFlash = 2 then Inc(FPickup, 5);
3570 if (not remove) and g_Game_IsNet then MH_SEND_Sound(GameX, GameY, 'SOUND_ITEM_GETITEM');
3571 end;
3573 ITEM_SUIT:
3574 if FMegaRulez[MR_SUIT] < gTime+PLAYER_SUIT_TIME then
3575 begin
3576 FMegaRulez[MR_SUIT] := gTime+PLAYER_SUIT_TIME;
3577 Result := True;
3578 remove := True;
3579 FFireTime := 0;
3580 if gFlash = 2 then Inc(FPickup, 5);
3581 end;
3583 ITEM_OXYGEN:
3584 if FAir < AIR_MAX then
3585 begin
3586 FAir := AIR_MAX;
3587 Result := True;
3588 remove := True;
3589 if gFlash = 2 then Inc(FPickup, 5);
3590 end;
3592 ITEM_MEDKIT_BLACK:
3593 begin
3594 if not (R_BERSERK in FRulez) then
3595 begin
3596 Include(FRulez, R_BERSERK);
3597 if allowBerserkSwitching then
3598 begin
3599 FCurrWeap := WEAPON_KASTET;
3600 resetWeaponQueue();
3601 FModel.SetWeapon(WEAPON_KASTET);
3602 end;
3603 if gFlash <> 0 then
3604 begin
3605 Inc(FPain, 100);
3606 if gFlash = 2 then Inc(FPickup, 5);
3607 end;
3608 FBerserk := gTime+30000;
3609 Result := True;
3610 remove := True;
3611 FFireTime := 0;
3612 end;
3613 if (FHealth < PLAYER_HP_SOFT) or (FFireTime > 0) then
3614 begin
3615 if FHealth < PLAYER_HP_SOFT then FHealth := PLAYER_HP_SOFT;
3616 FBerserk := gTime+30000;
3617 Result := True;
3618 remove := True;
3619 FFireTime := 0;
3620 end;
3621 end;
3623 ITEM_INVUL:
3624 if FMegaRulez[MR_INVUL] < gTime+PLAYER_INVUL_TIME then
3625 begin
3626 FMegaRulez[MR_INVUL] := gTime+PLAYER_INVUL_TIME;
3627 FSpawnInvul := 0;
3628 Result := True;
3629 remove := True;
3630 if gFlash = 2 then Inc(FPickup, 5);
3631 end;
3633 ITEM_BOTTLE:
3634 if (FHealth < PLAYER_HP_LIMIT) or (FFireTime > 0) then
3635 begin
3636 if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 4, PLAYER_HP_LIMIT);
3637 Result := True;
3638 remove := True;
3639 FFireTime := 0;
3640 if gFlash = 2 then Inc(FPickup, 5);
3641 end;
3643 ITEM_HELMET:
3644 if FArmor < PLAYER_AP_LIMIT then
3645 begin
3646 IncMax(FArmor, 5, PLAYER_AP_LIMIT);
3647 Result := True;
3648 remove := True;
3649 if gFlash = 2 then Inc(FPickup, 5);
3650 end;
3652 ITEM_JETPACK:
3653 if FJetFuel < JET_MAX then
3654 begin
3655 FJetFuel := JET_MAX;
3656 Result := True;
3657 remove := True;
3658 if gFlash = 2 then Inc(FPickup, 5);
3659 end;
3661 ITEM_INVIS:
3662 if FMegaRulez[MR_INVIS] < gTime+PLAYER_INVIS_TIME then
3663 begin
3664 FMegaRulez[MR_INVIS] := gTime+PLAYER_INVIS_TIME;
3665 Result := True;
3666 remove := True;
3667 if gFlash = 2 then Inc(FPickup, 5);
3668 end;
3669 end;
3670 end;
3672 procedure TPlayer.Touch();
3673 begin
3674 if not FAlive then
3675 Exit;
3676 //FModel.PlaySound(MODELSOUND_PAIN, 1, FObj.X, FObj.Y);
3677 if FIamBot then
3678 begin
3679 // Бросить флаг товарищу:
3680 if gGameSettings.GameMode = GM_CTF then
3681 DropFlag();
3682 end;
3683 end;
3685 procedure TPlayer.Push(vx, vy: Integer);
3686 begin
3687 if (not FPhysics) and FGhost then
3688 Exit;
3689 FObj.Accel.X := FObj.Accel.X + vx;
3690 FObj.Accel.Y := FObj.Accel.Y + vy;
3691 if g_Game_IsNet and g_Game_IsServer then
3692 MH_SEND_PlayerPos(True, FUID, NET_EVERYONE);
3693 end;
3695 procedure TPlayer.Reset(Force: Boolean);
3696 begin
3697 if Force then
3698 FAlive := False;
3700 FSpawned := False;
3701 FTime[T_RESPAWN] := 0;
3702 FTime[T_FLAGCAP] := 0;
3703 FGodMode := False;
3704 FNoTarget := False;
3705 FNoReload := False;
3706 FFrags := 0;
3707 FLastFrag := 0;
3708 FComboEvnt := -1;
3709 FKills := 0;
3710 FMonsterKills := 0;
3711 FDeath := 0;
3712 FSecrets := 0;
3713 FSpawnInvul := 0;
3714 FCorpse := -1;
3715 FReady := False;
3716 if FNoRespawn then
3717 begin
3718 FSpectator := False;
3719 FGhost := False;
3720 FPhysics := True;
3721 FSpectatePlayer := -1;
3722 FNoRespawn := False;
3723 end;
3724 FLives := gGameSettings.MaxLives;
3726 SetFlag(FLAG_NONE);
3727 end;
3729 procedure TPlayer.SoftReset();
3730 begin
3731 ReleaseKeys();
3733 FDamageBuffer := 0;
3734 FSlopeOld := 0;
3735 FIncCamOld := 0;
3736 FIncCam := 0;
3737 FBFGFireCounter := -1;
3738 FShellTimer := -1;
3739 FPain := 0;
3740 FLastHit := 0;
3741 FLastFrag := 0;
3742 FComboEvnt := -1;
3744 SetFlag(FLAG_NONE);
3745 SetAction(A_STAND, True);
3746 end;
3748 function TPlayer.GetRespawnPoint(): Byte;
3749 var
3750 c: Byte;
3751 begin
3752 Result := 255;
3753 // На будущее: FSpawn - игрок уже играл и перерождается
3755 // Одиночная игра/кооператив
3756 if gGameSettings.GameMode in [GM_COOP, GM_SINGLE] then
3757 begin
3758 if Self = gPlayer1 then
3759 begin
3760 // player 1 should try to spawn on the player 1 point
3761 if g_Map_GetPointCount(RESPAWNPOINT_PLAYER1) > 0 then
3762 Exit(RESPAWNPOINT_PLAYER1)
3763 else if g_Map_GetPointCount(RESPAWNPOINT_PLAYER2) > 0 then
3764 Exit(RESPAWNPOINT_PLAYER2);
3765 end
3766 else if Self = gPlayer2 then
3767 begin
3768 // player 2 should try to spawn on the player 2 point
3769 if g_Map_GetPointCount(RESPAWNPOINT_PLAYER2) > 0 then
3770 Exit(RESPAWNPOINT_PLAYER2)
3771 else if g_Map_GetPointCount(RESPAWNPOINT_PLAYER1) > 0 then
3772 Exit(RESPAWNPOINT_PLAYER1);
3773 end
3774 else
3775 begin
3776 // other players randomly pick either the first or the second point
3777 c := IfThen((Random(2) = 0), RESPAWNPOINT_PLAYER1, RESPAWNPOINT_PLAYER2);
3778 if g_Map_GetPointCount(c) > 0 then
3779 Exit(c);
3780 // try the other one
3781 c := IfThen((c = RESPAWNPOINT_PLAYER1), RESPAWNPOINT_PLAYER2, RESPAWNPOINT_PLAYER1);
3782 if g_Map_GetPointCount(c) > 0 then
3783 Exit(c);
3784 end;
3785 end;
3787 // Мясоповал
3788 if gGameSettings.GameMode = GM_DM then
3789 begin
3790 // try DM points first
3791 if g_Map_GetPointCount(RESPAWNPOINT_DM) > 0 then
3792 Exit(RESPAWNPOINT_DM);
3793 end;
3795 // Командные
3796 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
3797 begin
3798 // try team points first
3799 c := RESPAWNPOINT_DM;
3800 if FTeam = TEAM_RED then
3801 c := RESPAWNPOINT_RED
3802 else if FTeam = TEAM_BLUE then
3803 c := RESPAWNPOINT_BLUE;
3804 if g_Map_GetPointCount(c) > 0 then
3805 Exit(c);
3806 end;
3808 // still haven't found a spawnpoint, try random shit
3809 Result := g_Map_GetRandomPointType();
3810 end;
3812 procedure TPlayer.Respawn(Silent: Boolean; Force: Boolean = False);
3813 var
3814 RespawnPoint: TRespawnPoint;
3815 a, b, c: Byte;
3816 begin
3817 FSlopeOld := 0;
3818 FIncCamOld := 0;
3819 FIncCam := 0;
3820 FBFGFireCounter := -1;
3821 FShellTimer := -1;
3822 FPain := 0;
3823 FLastHit := 0;
3824 FSpawnInvul := 0;
3825 FCorpse := -1;
3827 if not g_Game_IsServer then
3828 Exit;
3829 if FDummy then
3830 Exit;
3831 FWantsInGame := True;
3832 FJustTeleported := True;
3833 if Force then
3834 begin
3835 FTime[T_RESPAWN] := 0;
3836 FAlive := False;
3837 end;
3838 FNetTime := 0;
3839 // if server changes MaxLives we gotta be ready
3840 if gGameSettings.MaxLives = 0 then FNoRespawn := False;
3842 // Еще нельзя возродиться:
3843 if FTime[T_RESPAWN] > gTime then
3844 Exit;
3846 // Просрал все жизни:
3847 if FNoRespawn then
3848 begin
3849 if not FSpectator then Spectate(True);
3850 FWantsInGame := True;
3851 Exit;
3852 end;
3854 if (gGameSettings.GameType <> GT_SINGLE) and (gGameSettings.GameMode <> GM_COOP) then
3855 begin // "Своя игра"
3856 // Берсерк не сохраняется между уровнями:
3857 FRulez := FRulez-[R_BERSERK];
3858 end
3859 else // "Одиночная игра"/"Кооп"
3860 begin
3861 // Берсерк и ключи не сохраняются между уровнями:
3862 FRulez := FRulez-[R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE, R_BERSERK];
3863 end;
3865 // Получаем точку спауна игрока:
3866 c := GetRespawnPoint();
3868 ReleaseKeys();
3869 SetFlag(FLAG_NONE);
3871 // Воскрешение без оружия:
3872 if not FAlive then
3873 begin
3874 FHealth := Round(PLAYER_HP_SOFT * (FHandicap / 100));
3875 FArmor := 0;
3876 FAlive := True;
3877 FAir := AIR_DEF;
3878 FJetFuel := 0;
3880 for a := WP_FIRST to WP_LAST do
3881 begin
3882 FWeapon[a] := False;
3883 FReloading[a] := 0;
3884 end;
3886 FWeapon[WEAPON_PISTOL] := True;
3887 FWeapon[WEAPON_KASTET] := True;
3888 FCurrWeap := WEAPON_PISTOL;
3889 resetWeaponQueue();
3891 FModel.SetWeapon(FCurrWeap);
3893 for b := A_BULLETS to A_HIGH do
3894 FAmmo[b] := 0;
3896 FAmmo[A_BULLETS] := 50;
3898 FMaxAmmo[A_BULLETS] := AmmoLimits[0, A_BULLETS];
3899 FMaxAmmo[A_SHELLS] := AmmoLimits[0, A_SHELLS];
3900 FMaxAmmo[A_ROCKETS] := AmmoLimits[0, A_SHELLS];
3901 FMaxAmmo[A_CELLS] := AmmoLimits[0, A_CELLS];
3902 FMaxAmmo[A_FUEL] := AmmoLimits[0, A_FUEL];
3904 if (gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF]) and
3905 LongBool(gGameSettings.Options and GAME_OPTION_DMKEYS) then
3906 FRulez := [R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE]
3907 else
3908 FRulez := [];
3909 end;
3911 // Получаем координаты точки возрождения:
3912 if not g_Map_GetPoint(c, RespawnPoint) then
3913 begin
3914 g_FatalError(_lc[I_GAME_ERROR_GET_SPAWN]);
3915 Exit;
3916 end;
3918 // Установка координат и сброс всех параметров:
3919 FObj.X := RespawnPoint.X-PLAYER_RECT.X;
3920 FObj.Y := RespawnPoint.Y-PLAYER_RECT.Y;
3921 FObj.oldX := FObj.X; // don't interpolate after respawn
3922 FObj.oldY := FObj.Y;
3923 FObj.Vel.X := 0;
3924 FObj.Vel.Y := 0;
3925 FObj.Accel.X := 0;
3926 FObj.Accel.Y := 0;
3928 FDirection := RespawnPoint.Direction;
3929 if FDirection = TDirection.D_LEFT then
3930 FAngle := 180
3931 else
3932 FAngle := 0;
3934 SetAction(A_STAND, True);
3935 FModel.Direction := FDirection;
3937 for a := Low(FTime) to High(FTime) do
3938 FTime[a] := 0;
3940 for a := Low(FMegaRulez) to High(FMegaRulez) do
3941 FMegaRulez[a] := 0;
3943 // Respawn invulnerability
3944 if (gGameSettings.GameType <> GT_SINGLE) and (gGameSettings.SpawnInvul > 0) then
3945 begin
3946 FMegaRulez[MR_INVUL] := gTime + gGameSettings.SpawnInvul * 1000;
3947 FSpawnInvul := FMegaRulez[MR_INVUL];
3948 end;
3950 FDamageBuffer := 0;
3951 FJetpack := False;
3952 FCanJetpack := False;
3953 FFlaming := False;
3954 FFireTime := 0;
3955 FFirePainTime := 0;
3956 FFireAttacker := 0;
3958 {$IFDEF ENABLE_GFX}
3959 // Анимация возрождения:
3960 if (not gLoadGameMode) and (not Silent) then
3961 begin
3962 g_GFX_QueueEffect(
3963 R_GFX_TELEPORT_FAST,
3964 FObj.X + PLAYER_RECT.X + (PLAYER_RECT.Width div 2) - 32,
3965 FObj.Y + PLAYER_RECT.Y + (PLAYER_RECT.Height div 2) - 32
3966 );
3967 end;
3968 {$ENDIF}
3970 FSpectator := False;
3971 FGhost := False;
3972 FPhysics := True;
3973 FSpectatePlayer := -1;
3974 FSpawned := True;
3976 if (gPlayer1 = nil) and (gSpectLatchPID1 = FUID) then
3977 gPlayer1 := self;
3978 if (gPlayer2 = nil) and (gSpectLatchPID2 = FUID) then
3979 gPlayer2 := self;
3981 if g_Game_IsNet then
3982 begin
3983 MH_SEND_PlayerPos(True, FUID, NET_EVERYONE);
3984 MH_SEND_PlayerStats(FUID, NET_EVERYONE);
3985 if not Silent then
3986 MH_SEND_Effect(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
3987 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32,
3988 0, NET_GFX_TELE);
3989 end;
3990 end;
3992 procedure TPlayer.Spectate(NoMove: Boolean = False);
3993 begin
3994 if FAlive then
3995 Kill(K_EXTRAHARDKILL, FUID, HIT_SOME)
3996 else if (not NoMove) then
3997 begin
3998 GameX := gMapInfo.Width div 2;
3999 GameY := gMapInfo.Height div 2;
4000 end;
4001 FXTo := GameX;
4002 FYTo := GameY;
4004 FAlive := False;
4005 FSpectator := True;
4006 FGhost := True;
4007 FPhysics := False;
4008 FWantsInGame := False;
4009 FSpawned := False;
4010 FCorpse := -1;
4012 if FNoRespawn then
4013 begin
4014 if Self = gPlayer1 then
4015 begin
4016 gSpectLatchPID1 := FUID;
4017 gPlayer1 := nil;
4018 end
4019 else if Self = gPlayer2 then
4020 begin
4021 gSpectLatchPID2 := FUID;
4022 gPlayer2 := nil;
4023 end;
4024 end;
4026 if g_Game_IsNet then
4027 MH_SEND_PlayerStats(FUID);
4028 end;
4030 procedure TPlayer.SwitchNoClip;
4031 begin
4032 if not FAlive then
4033 Exit;
4034 FGhost := not FGhost;
4035 FPhysics := not FGhost;
4036 if FGhost then
4037 begin
4038 FXTo := FObj.X;
4039 FYTo := FObj.Y;
4040 end else
4041 begin
4042 FObj.Accel.X := 0;
4043 FObj.Accel.Y := 0;
4044 end;
4045 end;
4047 procedure TPlayer.Run(Direction: TDirection);
4048 var
4049 a, b: Integer;
4050 begin
4051 if MAX_RUNVEL > 8 then
4052 FlySmoke();
4054 // Бежим:
4055 if Direction = TDirection.D_LEFT then
4056 begin
4057 if FObj.Vel.X > -MAX_RUNVEL then
4058 FObj.Vel.X := FObj.Vel.X - (MAX_RUNVEL shr 3);
4059 end
4060 else
4061 if FObj.Vel.X < MAX_RUNVEL then
4062 FObj.Vel.X := FObj.Vel.X + (MAX_RUNVEL shr 3);
4064 // Возможно, пинаем куски:
4065 if (FObj.Vel.X <> 0) and (gGibs <> nil) then
4066 begin
4067 b := Abs(FObj.Vel.X);
4068 if b > 1 then b := b * (Random(8 div b) + 1);
4069 for a := 0 to High(gGibs) do
4070 begin
4071 if gGibs[a].alive and
4072 g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y+FObj.Rect.Height-4,
4073 FObj.Rect.Width, 8, @gGibs[a].Obj) and (Random(3) = 0) then
4074 begin
4075 // Пинаем куски
4076 if FObj.Vel.X < 0 then
4077 begin
4078 g_Obj_PushA(@gGibs[a].Obj, b, Random(61)+120) // налево
4079 end
4080 else
4081 begin
4082 g_Obj_PushA(@gGibs[a].Obj, b, Random(61)); // направо
4083 end;
4084 gGibs[a].positionChanged(); // this updates spatial accelerators
4085 end;
4086 end;
4087 end;
4089 SetAction(A_WALK);
4090 end;
4092 procedure TPlayer.SeeDown();
4093 begin
4094 SetAction(A_SEEDOWN);
4096 if FDirection = TDirection.D_LEFT then FAngle := ANGLE_LEFTDOWN else FAngle := ANGLE_RIGHTDOWN;
4098 if FIncCam > -120 then DecMin(FIncCam, 5, -120);
4099 end;
4101 procedure TPlayer.SeeUp();
4102 begin
4103 SetAction(A_SEEUP);
4105 if FDirection = TDirection.D_LEFT then FAngle := ANGLE_LEFTUP else FAngle := ANGLE_RIGHTUP;
4107 if FIncCam < 120 then IncMax(FIncCam, 5, 120);
4108 end;
4110 procedure TPlayer.SetAction(Action: Byte; Force: Boolean = False);
4111 var
4112 Prior: Byte;
4113 begin
4114 case Action of
4115 A_WALK: Prior := 3;
4116 A_DIE1: Prior := 5;
4117 A_DIE2: Prior := 5;
4118 A_ATTACK: Prior := 2;
4119 A_SEEUP: Prior := 1;
4120 A_SEEDOWN: Prior := 1;
4121 A_ATTACKUP: Prior := 2;
4122 A_ATTACKDOWN: Prior := 2;
4123 A_PAIN: Prior := 4;
4124 else Prior := 0;
4125 end;
4127 if (Prior > FActionPrior) or Force then
4128 if not ((Prior = 2) and (FCurrWeap = WEAPON_SAW)) then
4129 begin
4130 FActionPrior := Prior;
4131 FActionAnim := Action;
4132 FActionForce := Force;
4133 FActionChanged := True;
4134 end;
4136 if Action in [A_ATTACK, A_ATTACKUP, A_ATTACKDOWN] then FModel.SetFire(True);
4137 end;
4139 function TPlayer.StayOnStep(XInc, YInc: Integer): Boolean;
4140 begin
4141 Result := not g_Map_CollidePanel(FObj.X+PLAYER_RECT.X, FObj.Y+YInc+PLAYER_RECT.Y+PLAYER_RECT.Height-1,
4142 PLAYER_RECT.Width, 1, PANEL_STEP, False)
4143 and g_Map_CollidePanel(FObj.X+PLAYER_RECT.X, FObj.Y+YInc+PLAYER_RECT.Y+PLAYER_RECT.Height,
4144 PLAYER_RECT.Width, 1, PANEL_STEP, False);
4145 end;
4147 function TPlayer.TeleportTo(X, Y: Integer; silent: Boolean; dir: Byte): Boolean;
4148 begin
4149 Result := False;
4151 if g_CollideLevel(X, Y, PLAYER_RECT.Width, PLAYER_RECT.Height) then
4152 begin
4153 g_Sound_PlayExAt('SOUND_GAME_NOTELEPORT', FObj.X, FObj.Y);
4154 if g_Game_IsServer and g_Game_IsNet then
4155 MH_SEND_Sound(FObj.X, FObj.Y, 'SOUND_GAME_NOTELEPORT');
4156 Exit;
4157 end;
4159 FJustTeleported := True;
4161 if not silent then
4162 begin
4163 g_Sound_PlayExAt('SOUND_GAME_TELEPORT', FObj.X, FObj.Y);
4164 {$IFDEF ENABLE_GFX}
4165 g_GFX_QueueEffect(
4166 R_GFX_TELEPORT_FAST,
4167 FObj.X + PLAYER_RECT.X + (PLAYER_RECT.Width div 2) - 32,
4168 FObj.Y + PLAYER_RECT.Y + (PLAYER_RECT.Height div 2) - 32
4169 );
4170 {$ENDIF}
4171 if g_Game_IsServer and g_Game_IsNet then
4172 MH_SEND_Effect(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
4173 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32, 1,
4174 NET_GFX_TELE);
4175 end;
4177 FObj.X := X-PLAYER_RECT.X;
4178 FObj.Y := Y-PLAYER_RECT.Y;
4179 FObj.oldX := FObj.X; // don't interpolate after respawn
4180 FObj.oldY := FObj.Y;
4181 if FAlive and FGhost then
4182 begin
4183 FXTo := FObj.X;
4184 FYTo := FObj.Y;
4185 end;
4187 if not g_Game_IsNet then
4188 begin
4189 if dir = 1 then
4190 begin
4191 SetDirection(TDirection.D_LEFT);
4192 FAngle := 180;
4193 end
4194 else
4195 if dir = 2 then
4196 begin
4197 SetDirection(TDirection.D_RIGHT);
4198 FAngle := 0;
4199 end
4200 else
4201 if dir = 3 then
4202 begin // обратное
4203 if FDirection = TDirection.D_RIGHT then
4204 begin
4205 SetDirection(TDirection.D_LEFT);
4206 FAngle := 180;
4207 end
4208 else
4209 begin
4210 SetDirection(TDirection.D_RIGHT);
4211 FAngle := 0;
4212 end;
4213 end;
4214 end;
4216 if not silent then
4217 begin
4218 {$IFDEF ENABLE_GFX}
4219 g_GFX_QueueEffect(
4220 R_GFX_TELEPORT_FAST,
4221 FObj.X + PLAYER_RECT.X + (PLAYER_RECT.Width div 2) - 32,
4222 FObj.Y + PLAYER_RECT.Y + (PLAYER_RECT.Height div 2) - 32
4223 );
4224 {$ENDIF}
4225 if g_Game_IsServer and g_Game_IsNet then
4226 MH_SEND_Effect(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2)-32,
4227 FObj.Y+PLAYER_RECT.Y+(PLAYER_RECT.Height div 2)-32, 0,
4228 NET_GFX_TELE);
4229 end;
4231 Result := True;
4232 end;
4234 function nonz(a: Single): Single;
4235 begin
4236 if a <> 0 then
4237 Result := a
4238 else
4239 Result := 1;
4240 end;
4242 function TPlayer.refreshCorpse(): Boolean;
4243 var
4244 i: Integer;
4245 begin
4246 Result := False;
4247 FCorpse := -1;
4248 if FAlive or FSpectator then
4249 Exit;
4250 if (gCorpses = nil) or (Length(gCorpses) = 0) then
4251 Exit;
4252 for i := 0 to High(gCorpses) do
4253 if gCorpses[i] <> nil then
4254 if gCorpses[i].FPlayerUID = FUID then
4255 begin
4256 Result := True;
4257 FCorpse := i;
4258 break;
4259 end;
4260 end;
4262 function TPlayer.getCameraObj(): TObj;
4263 begin
4264 if (not FAlive) and (not FSpectator) and
4265 (FCorpse >= 0) and (FCorpse < Length(gCorpses)) and
4266 (gCorpses[FCorpse] <> nil) and (gCorpses[FCorpse].FPlayerUID = FUID) then
4267 begin
4268 gCorpses[FCorpse].FObj.slopeUpLeft := FObj.slopeUpLeft;
4269 Result := gCorpses[FCorpse].FObj;
4270 end
4271 else
4272 begin
4273 Result := FObj;
4274 end;
4275 end;
4277 procedure TPlayer.PreUpdate();
4278 begin
4279 FSlopeOld := FObj.slopeUpLeft;
4280 FIncCamOld := FIncCam;
4281 FObj.oldX := FObj.X;
4282 FObj.oldY := FObj.Y;
4283 end;
4285 procedure TPlayer.Update();
4286 var
4287 b: Byte;
4288 i, ii, wx, wy, xd, yd, k: Integer;
4289 blockmon, headwater, dospawn: Boolean;
4290 NetServer: Boolean;
4291 AnyServer: Boolean;
4292 SetSpect: Boolean;
4293 begin
4294 NetServer := g_Game_IsNet and g_Game_IsServer;
4295 AnyServer := g_Game_IsServer;
4297 if g_Game_IsClient and (NetInterpLevel > 0) then
4298 DoLerp(NetInterpLevel + 1)
4299 else
4300 if FGhost then
4301 DoLerp(4);
4303 if NetServer then
4304 if FClientID >= 0 then
4305 begin
4306 FPing := NetClients[FClientID].Peer^.lastRoundTripTime;
4307 if NetClients[FClientID].Peer^.packetsSent > 0 then
4308 FLoss := Round(100*NetClients[FClientID].Peer^.packetsLost/NetClients[FClientID].Peer^.packetsSent)
4309 else
4310 FLoss := 0;
4311 end else
4312 begin
4313 FPing := 0;
4314 FLoss := 0;
4315 end;
4317 if FAlive then
4318 FPunchAnim.Update;
4319 if FPunchAnim.played then
4320 FPunchAnim.Disable;
4322 if FAlive and (gFly or FJetpack) then
4323 FlySmoke();
4325 if FDirection = TDirection.D_LEFT then
4326 FAngle := 180
4327 else
4328 FAngle := 0;
4330 if FAlive and (not FGhost) then
4331 begin
4332 if FKeys[KEY_UP].Pressed then
4333 SeeUp();
4334 if FKeys[KEY_DOWN].Pressed then
4335 SeeDown();
4336 end;
4338 if (not (FKeys[KEY_UP].Pressed or FKeys[KEY_DOWN].Pressed)) and
4339 (FIncCam <> 0) then
4340 begin
4341 i := g_basic.Sign(FIncCam);
4342 FIncCam := Abs(FIncCam);
4343 DecMin(FIncCam, 5, 0);
4344 FIncCam := FIncCam*i;
4345 end;
4347 // no need to do that each second frame, weapon queue will take care of it
4348 if FAlive and FKeys[KEY_NEXTWEAPON].Pressed and AnyServer then NextWeapon();
4349 if FAlive and FKeys[KEY_PREVWEAPON].Pressed and AnyServer then PrevWeapon();
4351 if gTime mod (GAME_TICK*2) <> 0 then
4352 begin
4353 if (FObj.Vel.X = 0) and FAlive then
4354 begin
4355 if FKeys[KEY_LEFT].Pressed then
4356 Run(TDirection.D_LEFT);
4357 if FKeys[KEY_RIGHT].Pressed then
4358 Run(TDirection.D_RIGHT);
4359 end;
4361 if FPhysics then
4362 begin
4363 g_Obj_Move(@FObj, True, True, True);
4364 positionChanged(); // this updates spatial accelerators
4365 end;
4367 Exit;
4368 end;
4370 FActionChanged := False;
4372 if FAlive then
4373 begin
4374 // Let alive player do some actions
4375 if FKeys[KEY_LEFT].Pressed then Run(TDirection.D_LEFT);
4376 if FKeys[KEY_RIGHT].Pressed then Run(TDirection.D_RIGHT);
4377 //if FKeys[KEY_NEXTWEAPON].Pressed and AnyServer then NextWeapon();
4378 //if FKeys[KEY_PREVWEAPON].Pressed and AnyServer then PrevWeapon();
4379 if FKeys[KEY_FIRE].Pressed and AnyServer then Fire()
4380 else
4381 begin
4382 if AnyServer then
4383 begin
4384 FlamerOff;
4385 if NetServer then MH_SEND_PlayerStats(FUID);
4386 end;
4387 end;
4388 if FKeys[KEY_OPEN].Pressed and AnyServer then Use();
4389 if FKeys[KEY_JUMP].Pressed then Jump()
4390 else
4391 begin
4392 if AnyServer and FJetpack then
4393 begin
4394 FJetpack := False;
4395 JetpackOff;
4396 if NetServer then MH_SEND_PlayerStats(FUID);
4397 end;
4398 FCanJetpack := True;
4399 end;
4400 end
4401 else // Dead
4402 begin
4403 dospawn := False;
4404 if not FGhost then
4405 for k := Low(FKeys) to KEY_CHAT-1 do
4406 begin
4407 if FKeys[k].Pressed then
4408 begin
4409 dospawn := True;
4410 break;
4411 end;
4412 end;
4413 if dospawn then
4414 begin
4415 if gGameSettings.GameType in [GT_CUSTOM, GT_SERVER, GT_CLIENT] then
4416 Respawn(False)
4417 else // Single
4418 if (FTime[T_RESPAWN] <= gTime) and
4419 gGameOn and (not FAlive) then
4420 begin
4421 if (g_Player_GetCount() > 1) then
4422 Respawn(False)
4423 else
4424 begin
4425 gExit := EXIT_RESTART;
4426 Exit;
4427 end;
4428 end;
4429 end;
4430 // Dead spectator actions
4431 if FGhost then
4432 begin
4433 if FKeys[KEY_OPEN].Pressed and AnyServer then Fire();
4434 if FKeys[KEY_FIRE].Pressed and AnyServer then
4435 begin
4436 if FSpectator then
4437 begin
4438 if (FSpectatePlayer >= High(gPlayers)) then
4439 FSpectatePlayer := -1
4440 else
4441 begin
4442 SetSpect := False;
4443 for I := FSpectatePlayer + 1 to High(gPlayers) do
4444 if gPlayers[I] <> nil then
4445 if gPlayers[I].alive then
4446 if gPlayers[I].UID <> FUID then
4447 begin
4448 FSpectatePlayer := I;
4449 SetSpect := True;
4450 break;
4451 end;
4453 if not SetSpect then FSpectatePlayer := -1;
4454 end;
4456 ReleaseKeys;
4457 end;
4458 end;
4459 end;
4460 end;
4461 // No clipping
4462 if FGhost then
4463 begin
4464 if FKeys[KEY_UP].Pressed or FKeys[KEY_JUMP].Pressed then
4465 begin
4466 FYTo := FObj.Y - 32;
4467 FSpectatePlayer := -1;
4468 end;
4469 if FKeys[KEY_DOWN].Pressed then
4470 begin
4471 FYTo := FObj.Y + 32;
4472 FSpectatePlayer := -1;
4473 end;
4474 if FKeys[KEY_LEFT].Pressed then
4475 begin
4476 FXTo := FObj.X - 32;
4477 FSpectatePlayer := -1;
4478 end;
4479 if FKeys[KEY_RIGHT].Pressed then
4480 begin
4481 FXTo := FObj.X + 32;
4482 FSpectatePlayer := -1;
4483 end;
4485 if (FXTo < -64) then
4486 FXTo := -64
4487 else if (FXTo > gMapInfo.Width + 32) then
4488 FXTo := gMapInfo.Width + 32;
4489 if (FYTo < -72) then
4490 FYTo := -72
4491 else if (FYTo > gMapInfo.Height + 32) then
4492 FYTo := gMapInfo.Height + 32;
4493 end;
4495 if FPhysics then
4496 begin
4497 g_Obj_Move(@FObj, True, True, True);
4498 positionChanged(); // this updates spatial accelerators
4499 end
4500 else
4501 begin
4502 FObj.Vel.X := 0;
4503 FObj.Vel.Y := 0;
4504 if FSpectator then
4505 if (FSpectatePlayer <= High(gPlayers)) and (FSpectatePlayer >= 0) then
4506 if gPlayers[FSpectatePlayer] <> nil then
4507 if gPlayers[FSpectatePlayer].alive then
4508 begin
4509 FXTo := gPlayers[FSpectatePlayer].GameX;
4510 FYTo := gPlayers[FSpectatePlayer].GameY;
4511 end;
4512 end;
4514 blockmon := g_Map_CollidePanel(FObj.X+PLAYER_HEADRECT.X, FObj.Y+PLAYER_HEADRECT.Y,
4515 PLAYER_HEADRECT.Width, PLAYER_HEADRECT.Height,
4516 PANEL_BLOCKMON, True);
4517 headwater := HeadInLiquid(0, 0);
4519 // Сопротивление воздуха:
4520 if (not FAlive) or not (FKeys[KEY_LEFT].Pressed or FKeys[KEY_RIGHT].Pressed) then
4521 if FObj.Vel.X <> 0 then
4522 FObj.Vel.X := z_dec(FObj.Vel.X, 1);
4524 if (FLastHit = HIT_TRAP) and (FPain > 90) then FPain := 90;
4525 DecMin(FPain, 5, 0);
4526 DecMin(FPickup, 1, 0);
4528 if FAlive and (FObj.Y > Integer(gMapInfo.Height)+128) and AnyServer then
4529 begin
4530 // Обнулить действия примочек, чтобы фон пропал
4531 FMegaRulez[MR_SUIT] := 0;
4532 FMegaRulez[MR_INVUL] := 0;
4533 FMegaRulez[MR_INVIS] := 0;
4534 Kill(K_FALLKILL, 0, HIT_FALL);
4535 end;
4537 i := 9;
4539 if FAlive then
4540 begin
4541 if FCurrWeap = WEAPON_SAW then
4542 if not (FSawSound.IsPlaying() or FSawSoundHit.IsPlaying() or
4543 FSawSoundSelect.IsPlaying()) then
4544 FSawSoundIdle.PlayAt(FObj.X, FObj.Y);
4546 if FJetpack then
4547 if (not FJetSoundFly.IsPlaying()) and (not FJetSoundOn.IsPlaying()) and
4548 (not FJetSoundOff.IsPlaying()) then
4549 begin
4550 FJetSoundFly.SetPosition(0);
4551 FJetSoundFly.PlayAt(FObj.X, FObj.Y);
4552 end;
4554 for b := WP_FIRST to WP_LAST do
4555 if FReloading[b] > 0 then
4556 if FNoReload then
4557 FReloading[b] := 0
4558 else
4559 Dec(FReloading[b]);
4561 if FShellTimer > -1 then
4562 if FShellTimer = 0 then
4563 begin
4564 if FShellType = SHELL_SHELL then
4565 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4566 GameVelX, GameVelY-2, SHELL_SHELL)
4567 else if FShellType = SHELL_DBLSHELL then
4568 begin
4569 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4570 GameVelX+1, GameVelY-2, SHELL_SHELL);
4571 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4572 GameVelX-1, GameVelY-2, SHELL_SHELL);
4573 end;
4574 FShellTimer := -1;
4575 end else Dec(FShellTimer);
4577 if (FBFGFireCounter > -1) then
4578 if FBFGFireCounter = 0 then
4579 begin
4580 if AnyServer then
4581 begin
4582 wx := FObj.X+WEAPONPOINT[FDirection].X;
4583 wy := FObj.Y+WEAPONPOINT[FDirection].Y;
4584 xd := wx+IfThen(FDirection = TDirection.D_LEFT, -30, 30);
4585 yd := wy+firediry();
4586 g_Weapon_bfgshot(wx, wy, xd, yd, FUID);
4587 if NetServer then MH_SEND_PlayerFire(FUID, WEAPON_BFG, wx, wy, xd, yd);
4588 if (FAngle = 0) or (FAngle = 180) then SetAction(A_ATTACK)
4589 else if (FAngle = ANGLE_LEFTDOWN) or (FAngle = ANGLE_RIGHTDOWN) then SetAction(A_ATTACKDOWN)
4590 else if (FAngle = ANGLE_LEFTUP) or (FAngle = ANGLE_RIGHTUP) then SetAction(A_ATTACKUP);
4591 end;
4593 FReloading[WEAPON_BFG] := WEAPON_RELOAD[WEAPON_BFG];
4594 FBFGFireCounter := -1;
4595 end else
4596 if FNoReload then
4597 FBFGFireCounter := 0
4598 else
4599 Dec(FBFGFireCounter);
4601 if (FMegaRulez[MR_SUIT] < gTime) and AnyServer then
4602 begin
4603 b := g_GetAcidHit(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y, PLAYER_RECT.Width, PLAYER_RECT.Height);
4605 if (b > 0) and (gTime mod (15*GAME_TICK) = 0) then Damage(b, 0, 0, 0, HIT_ACID);
4606 end;
4608 if (headwater or blockmon) then
4609 begin
4610 Dec(FAir);
4612 if FAir < -9 then
4613 begin
4614 if AnyServer then Damage(10, 0, 0, 0, HIT_WATER);
4615 FAir := 0;
4616 end
4617 else if (FAir mod 31 = 0) and not blockmon then
4618 begin
4619 {$IFDEF ENABLE_GFX}
4620 g_GFX_Bubbles(FObj.X+PLAYER_RECT.X+(PLAYER_RECT.Width div 2), FObj.Y+PLAYER_RECT.Y-4, 5+Random(6), 8, 4);
4621 {$ENDIF}
4622 if Random(2) = 0 then
4623 g_Sound_PlayExAt('SOUND_GAME_BUBBLE1', FObj.X, FObj.Y)
4624 else
4625 g_Sound_PlayExAt('SOUND_GAME_BUBBLE2', FObj.X, FObj.Y);
4626 end;
4627 end else if FAir < AIR_DEF then
4628 FAir := AIR_DEF;
4630 if FFireTime > 0 then
4631 begin
4632 if BodyInLiquid(0, 0) then
4633 begin
4634 FFireTime := 0;
4635 FFirePainTime := 0;
4636 end
4637 else if FMegaRulez[MR_SUIT] >= gTime then
4638 begin
4639 if FMegaRulez[MR_SUIT] = gTime then
4640 FFireTime := 1;
4641 FFirePainTime := 0;
4642 end
4643 else
4644 begin
4645 OnFireFlame(1);
4646 if FFirePainTime <= 0 then
4647 begin
4648 if g_Game_IsServer then
4649 Damage(2, FFireAttacker, 0, 0, HIT_FLAME);
4650 FFirePainTime := 12 - FFireTime div 12;
4651 end;
4652 FFirePainTime := FFirePainTime - 1;
4653 FFireTime := FFireTime - 1;
4654 if ((FFireTime mod 33) = 0) and (FMegaRulez[MR_INVUL] < gTime) then
4655 FModel.PlaySound(MODELSOUND_PAIN, 1, FObj.X, FObj.Y);
4656 if (FFireTime = 0) and g_Game_IsNet and g_Game_IsServer then
4657 MH_SEND_PlayerStats(FUID);
4658 end;
4659 end;
4661 if FDamageBuffer > 0 then
4662 begin
4663 if FDamageBuffer >= 9 then
4664 begin
4665 SetAction(A_PAIN);
4667 if FDamageBuffer < 30 then i := 9
4668 else if FDamageBuffer < 100 then i := 18
4669 else i := 27;
4670 end;
4672 ii := Round(FDamageBuffer*FHealth / nonz(FArmor*(3/4)+FHealth));
4673 FArmor := FArmor-(FDamageBuffer-ii);
4674 FHealth := FHealth-ii;
4675 if FArmor < 0 then
4676 begin
4677 FHealth := FHealth+FArmor;
4678 FArmor := 0;
4679 end;
4681 if AnyServer then
4682 if FHealth <= 0 then
4683 if FHealth > -30 then Kill(K_SIMPLEKILL, FLastSpawnerUID, FLastHit)
4684 else if FHealth > -50 then Kill(K_HARDKILL, FLastSpawnerUID, FLastHit)
4685 else Kill(K_EXTRAHARDKILL, FLastSpawnerUID, FLastHit);
4687 if FAlive and ((FLastHit <> HIT_FLAME) or (FFireTime <= 0)) then
4688 begin
4689 if FDamageBuffer <= 20 then FModel.PlaySound(MODELSOUND_PAIN, 1, FObj.X, FObj.Y)
4690 else if FDamageBuffer <= 55 then FModel.PlaySound(MODELSOUND_PAIN, 2, FObj.X, FObj.Y)
4691 else if FDamageBuffer <= 120 then FModel.PlaySound(MODELSOUND_PAIN, 3, FObj.X, FObj.Y)
4692 else FModel.PlaySound(MODELSOUND_PAIN, 4, FObj.X, FObj.Y);
4693 end;
4695 FDamageBuffer := 0;
4696 end;
4698 {CollideItem();}
4699 end; // if FAlive then ...
4701 if (FActionAnim = A_PAIN) and (FModel.Animation <> A_PAIN) then
4702 begin
4703 FModel.ChangeAnimation(FActionAnim, FActionForce);
4704 FModel.AnimState.MinLength := i;
4705 end else FModel.ChangeAnimation(FActionAnim, FActionForce and (FModel.Animation <> A_STAND));
4707 if (FModel.AnimState.Played or ((not FActionChanged) and (FModel.Animation = A_WALK)))
4708 then SetAction(A_STAND, True);
4710 if not ((FModel.Animation = A_WALK) and (Abs(FObj.Vel.X) < 4) and not FModel.GetFire()) then FModel.Update;
4712 for b := Low(FKeys) to High(FKeys) do
4713 if FKeys[b].Time = 0 then FKeys[b].Pressed := False else Dec(FKeys[b].Time);
4714 end;
4717 procedure TPlayer.getMapBox (out x, y, w, h: Integer); inline;
4718 begin
4719 x := FObj.X+PLAYER_RECT.X;
4720 y := FObj.Y+PLAYER_RECT.Y;
4721 w := PLAYER_RECT.Width;
4722 h := PLAYER_RECT.Height;
4723 end;
4726 procedure TPlayer.moveBy (dx, dy: Integer); inline;
4727 begin
4728 if (dx <> 0) or (dy <> 0) then
4729 begin
4730 FObj.X += dx;
4731 FObj.Y += dy;
4732 positionChanged();
4733 end;
4734 end;
4737 function TPlayer.Collide(X, Y: Integer; Width, Height: Word): Boolean;
4738 begin
4739 Result := g_Collide(FObj.X+PLAYER_RECT.X,
4740 FObj.Y+PLAYER_RECT.Y,
4741 PLAYER_RECT.Width,
4742 PLAYER_RECT.Height,
4743 X, Y,
4744 Width, Height);
4745 end;
4747 function TPlayer.Collide(Panel: TPanel): Boolean;
4748 begin
4749 Result := g_Collide(FObj.X+PLAYER_RECT.X,
4750 FObj.Y+PLAYER_RECT.Y,
4751 PLAYER_RECT.Width,
4752 PLAYER_RECT.Height,
4753 Panel.X, Panel.Y,
4754 Panel.Width, Panel.Height);
4755 end;
4757 function TPlayer.Collide(X, Y: Integer): Boolean;
4758 begin
4759 X := X-FObj.X-PLAYER_RECT.X;
4760 Y := Y-FObj.Y-PLAYER_RECT.Y;
4761 Result := (x >= 0) and (x <= PLAYER_RECT.Width) and
4762 (y >= 0) and (y <= PLAYER_RECT.Height);
4763 end;
4765 function g_Player_ValidName(Name: string): Boolean;
4766 var
4767 a: Integer;
4768 begin
4769 Result := True;
4771 if gPlayers = nil then Exit;
4773 for a := 0 to High(gPlayers) do
4774 if gPlayers[a] <> nil then
4775 if LowerCase(Name) = LowerCase(gPlayers[a].FName) then
4776 begin
4777 Result := False;
4778 Exit;
4779 end;
4780 end;
4782 procedure TPlayer.SetDirection(Direction: TDirection);
4783 var
4784 d: TDirection;
4785 begin
4786 d := FModel.Direction;
4788 FModel.Direction := Direction;
4789 if d <> Direction then FModel.ChangeAnimation(FModel.Animation, True);
4791 FDirection := Direction;
4792 end;
4794 function TPlayer.GetKeys(): Byte;
4795 begin
4796 Result := 0;
4798 if R_KEY_RED in FRulez then Result := KEY_RED;
4799 if R_KEY_GREEN in FRulez then Result := Result or KEY_GREEN;
4800 if R_KEY_BLUE in FRulez then Result := Result or KEY_BLUE;
4802 if FTeam = TEAM_RED then Result := Result or KEY_REDTEAM;
4803 if FTeam = TEAM_BLUE then Result := Result or KEY_BLUETEAM;
4804 end;
4806 procedure TPlayer.Use();
4807 var
4808 a: Integer;
4809 begin
4810 if FTime[T_USE] > gTime then Exit;
4812 g_Triggers_PressR(FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y, PLAYER_RECT.Width,
4813 PLAYER_RECT.Height, FUID, ACTIVATE_PLAYERPRESS);
4815 for a := 0 to High(gPlayers) do
4816 if (gPlayers[a] <> nil) and (gPlayers[a] <> Self) and
4817 gPlayers[a].alive and SameTeam(FUID, gPlayers[a].FUID) and
4818 g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y,
4819 FObj.Rect.Width, FObj.Rect.Height, @gPlayers[a].FObj) then
4820 begin
4821 gPlayers[a].Touch();
4822 if g_Game_IsNet and g_Game_IsServer then
4823 MH_SEND_GameEvent(NET_EV_PLAYER_TOUCH, gPlayers[a].FUID);
4824 end;
4826 FTime[T_USE] := gTime+120;
4827 end;
4829 procedure TPlayer.NetFire(Wpn: Byte; X, Y, AX, AY: Integer; WID: Integer = -1);
4830 var
4831 locObj: TObj;
4832 F: Boolean;
4833 WX, WY, XD, YD: Integer;
4834 begin
4835 F := False;
4836 WX := X;
4837 WY := Y;
4838 XD := AX;
4839 YD := AY;
4841 case FCurrWeap of
4842 WEAPON_KASTET:
4843 begin
4844 DoPunch();
4845 if R_BERSERK in FRulez then
4846 begin
4847 //g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 75, FUID);
4848 locobj.X := FObj.X+FObj.Rect.X;
4849 locobj.Y := FObj.Y+FObj.Rect.Y;
4850 locobj.rect.X := 0;
4851 locobj.rect.Y := 0;
4852 locobj.rect.Width := 39;
4853 locobj.rect.Height := 52;
4854 locobj.Vel.X := (xd-wx) div 2;
4855 locobj.Vel.Y := (yd-wy) div 2;
4856 locobj.Accel.X := xd-wx;
4857 locobj.Accel.y := yd-wy;
4859 if g_Weapon_Hit(@locobj, 50, FUID, HIT_SOME) <> 0 then
4860 g_Sound_PlayExAt('SOUND_WEAPON_HITBERSERK', FObj.X, FObj.Y)
4861 else
4862 g_Sound_PlayExAt('SOUND_WEAPON_MISSBERSERK', FObj.X, FObj.Y);
4864 if gFlash = 1 then
4865 if FPain < 50 then
4866 FPain := min(FPain + 25, 50);
4867 end else
4868 g_Weapon_punch(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, 3, FUID);
4869 end;
4871 WEAPON_SAW:
4872 begin
4873 if g_Weapon_chainsaw(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y,
4874 IfThen(gGameSettings.GameMode in [GM_DM, GM_TDM, GM_CTF], 9, 3), FUID) <> 0 then
4875 begin
4876 FSawSoundSelect.Stop();
4877 FSawSound.Stop();
4878 FSawSoundHit.PlayAt(FObj.X, FObj.Y);
4879 end
4880 else if not FSawSoundHit.IsPlaying() then
4881 begin
4882 FSawSoundSelect.Stop();
4883 FSawSound.PlayAt(FObj.X, FObj.Y);
4884 end;
4885 f := True;
4886 end;
4888 WEAPON_PISTOL:
4889 begin
4890 g_Sound_PlayExAt('SOUND_WEAPON_FIREPISTOL', GameX, Gamey);
4891 FFireAngle := FAngle;
4892 f := True;
4893 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4894 GameVelX, GameVelY-2, SHELL_BULLET);
4895 end;
4897 WEAPON_SHOTGUN1:
4898 begin
4899 g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN', Gamex, Gamey);
4900 FFireAngle := FAngle;
4901 f := True;
4902 FShellTimer := 10;
4903 FShellType := SHELL_SHELL;
4904 end;
4906 WEAPON_SHOTGUN2:
4907 begin
4908 g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN2', Gamex, Gamey);
4909 FFireAngle := FAngle;
4910 f := True;
4911 FShellTimer := 13;
4912 FShellType := SHELL_DBLSHELL;
4913 end;
4915 WEAPON_CHAINGUN:
4916 begin
4917 g_Sound_PlayExAt('SOUND_WEAPON_FIRECGUN', Gamex, Gamey);
4918 FFireAngle := FAngle;
4919 f := True;
4920 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4921 GameVelX, GameVelY-2, SHELL_BULLET);
4922 end;
4924 WEAPON_ROCKETLAUNCHER:
4925 begin
4926 g_Weapon_Rocket(wx, wy, xd, yd, FUID, WID);
4927 FFireAngle := FAngle;
4928 f := True;
4929 end;
4931 WEAPON_PLASMA:
4932 begin
4933 g_Weapon_Plasma(wx, wy, xd, yd, FUID, WID);
4934 FFireAngle := FAngle;
4935 f := True;
4936 end;
4938 WEAPON_BFG:
4939 begin
4940 g_Weapon_BFGShot(wx, wy, xd, yd, FUID, WID);
4941 FFireAngle := FAngle;
4942 f := True;
4943 end;
4945 WEAPON_SUPERPULEMET:
4946 begin
4947 g_Sound_PlayExAt('SOUND_WEAPON_FIRESHOTGUN', Gamex, Gamey);
4948 FFireAngle := FAngle;
4949 f := True;
4950 g_Player_CreateShell(GameX+PLAYER_RECT_CX, GameY+PLAYER_RECT_CX,
4951 GameVelX, GameVelY-2, SHELL_SHELL);
4952 end;
4954 WEAPON_FLAMETHROWER:
4955 begin
4956 g_Weapon_flame(wx, wy, xd, yd, FUID, WID);
4957 FlamerOn;
4958 FFireAngle := FAngle;
4959 f := True;
4960 end;
4961 end;
4963 if not f then Exit;
4965 if (FAngle = 0) or (FAngle = 180) then SetAction(A_ATTACK)
4966 else if (FAngle = ANGLE_LEFTDOWN) or (FAngle = ANGLE_RIGHTDOWN) then SetAction(A_ATTACKDOWN)
4967 else if (FAngle = ANGLE_LEFTUP) or (FAngle = ANGLE_RIGHTUP) then SetAction(A_ATTACKUP);
4968 end;
4970 procedure TPlayer.DoLerp(Level: Integer = 2);
4971 begin
4972 if FObj.X <> FXTo then FObj.X := Lerp(FObj.X, FXTo, Level);
4973 if FObj.Y <> FYTo then FObj.Y := Lerp(FObj.Y, FYTo, Level);
4974 end;
4976 procedure TPlayer.SetLerp(XTo, YTo: Integer);
4977 var
4978 AX, AY: Integer;
4979 begin
4980 FXTo := XTo;
4981 FYTo := YTo;
4982 if FJustTeleported or (NetInterpLevel < 1) then
4983 begin
4984 FObj.X := XTo;
4985 FObj.Y := YTo;
4986 if FJustTeleported then
4987 begin
4988 FObj.oldX := FObj.X;
4989 FObj.oldY := FObj.Y;
4990 end;
4991 end
4992 else
4993 begin
4994 AX := Abs(FXTo - FObj.X);
4995 AY := Abs(FYTo - FObj.Y);
4996 if (AX > 32) or (AX <= NetInterpLevel) then
4997 FObj.X := FXTo;
4998 if (AY > 32) or (AY <= NetInterpLevel) then
4999 FObj.Y := FYTo;
5000 end;
5001 end;
5003 function TPlayer.FullInLift(XInc, YInc: Integer): Integer;
5004 begin
5005 if g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
5006 PLAYER_RECT.Width, PLAYER_RECT.Height-8,
5007 PANEL_LIFTUP, False) then Result := -1
5008 else
5009 if g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
5010 PLAYER_RECT.Width, PLAYER_RECT.Height-8,
5011 PANEL_LIFTDOWN, False) then Result := 1
5012 else Result := 0;
5013 end;
5015 function TPlayer.GetFlag(Flag: Byte): Boolean;
5016 var
5017 s, ts: String;
5018 evtype, a: Byte;
5019 begin
5020 Result := False;
5022 if Flag = FLAG_NONE then
5023 Exit;
5025 if not g_Game_IsServer then Exit;
5027 // Принес чужой флаг на свою базу:
5028 if (Flag = FTeam) and
5029 (gFlags[Flag].State = FLAG_STATE_NORMAL) and
5030 (FFlag <> FLAG_NONE) then
5031 begin
5032 if FFlag = FLAG_RED then
5033 s := _lc[I_PLAYER_FLAG_RED]
5034 else
5035 s := _lc[I_PLAYER_FLAG_BLUE];
5037 evtype := FLAG_STATE_SCORED;
5039 ts := Format('%.4d', [gFlags[FFlag].CaptureTime]);
5040 Insert('.', ts, Length(ts) + 1 - 3);
5041 g_Console_Add(Format(_lc[I_PLAYER_FLAG_CAPTURE], [FName, s, ts]), True);
5043 g_Map_ResetFlag(FFlag);
5044 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_CAPTURE], [AnsiUpperCase(s)]), 144);
5046 if ((Self = gPlayer1) or (Self = gPlayer2)
5047 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5048 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5049 a := 0
5050 else
5051 a := 1;
5053 if not sound_cap_flag[a].IsPlaying() then
5054 sound_cap_flag[a].Play();
5056 gTeamStat[FTeam].Goals := gTeamStat[FTeam].Goals + 1;
5058 Result := True;
5059 if g_Game_IsNet then
5060 begin
5061 MH_SEND_FlagEvent(evtype, FFlag, FUID, False);
5062 MH_SEND_GameStats;
5063 end;
5065 gFlags[FFlag].CaptureTime := 0;
5066 SetFlag(FLAG_NONE);
5067 Exit;
5068 end;
5070 // Подобрал свой флаг - вернул его на базу:
5071 if (Flag = FTeam) and
5072 (gFlags[Flag].State = FLAG_STATE_DROPPED) then
5073 begin
5074 if Flag = FLAG_RED then
5075 s := _lc[I_PLAYER_FLAG_RED]
5076 else
5077 s := _lc[I_PLAYER_FLAG_BLUE];
5079 evtype := FLAG_STATE_RETURNED;
5080 gFlags[Flag].CaptureTime := 0;
5082 g_Console_Add(Format(_lc[I_PLAYER_FLAG_RETURN], [FName, s]), True);
5084 g_Map_ResetFlag(Flag);
5085 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_RETURN], [AnsiUpperCase(s)]), 144);
5087 if ((Self = gPlayer1) or (Self = gPlayer2)
5088 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5089 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5090 a := 0
5091 else
5092 a := 1;
5094 if not sound_ret_flag[a].IsPlaying() then
5095 sound_ret_flag[a].Play();
5097 Result := True;
5098 if g_Game_IsNet then
5099 begin
5100 MH_SEND_FlagEvent(evtype, Flag, FUID, False);
5101 MH_SEND_GameStats;
5102 end;
5103 Exit;
5104 end;
5106 // Подобрал чужой флаг:
5107 if (Flag <> FTeam) and (FTime[T_FLAGCAP] <= gTime) then
5108 begin
5109 SetFlag(Flag);
5111 if Flag = FLAG_RED then
5112 s := _lc[I_PLAYER_FLAG_RED]
5113 else
5114 s := _lc[I_PLAYER_FLAG_BLUE];
5116 evtype := FLAG_STATE_CAPTURED;
5118 g_Console_Add(Format(_lc[I_PLAYER_FLAG_GET], [FName, s]), True);
5120 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_GET], [AnsiUpperCase(s)]), 144);
5122 gFlags[Flag].State := FLAG_STATE_CAPTURED;
5124 if ((Self = gPlayer1) or (Self = gPlayer2)
5125 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5126 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5127 a := 0
5128 else
5129 a := 1;
5131 if not sound_get_flag[a].IsPlaying() then
5132 sound_get_flag[a].Play();
5134 Result := True;
5135 if g_Game_IsNet then
5136 begin
5137 MH_SEND_FlagEvent(evtype, Flag, FUID, False);
5138 MH_SEND_GameStats;
5139 end;
5140 end;
5141 end;
5143 procedure TPlayer.SetFlag(Flag: Byte);
5144 begin
5145 FFlag := Flag;
5146 if FModel <> nil then
5147 FModel.SetFlag(FFlag);
5148 end;
5150 function TPlayer.DropFlag(Silent: Boolean = True): Boolean;
5151 var
5152 s: String;
5153 a: Byte;
5154 begin
5155 Result := False;
5156 if (not g_Game_IsServer) or (FFlag = FLAG_NONE) then
5157 Exit;
5158 FTime[T_FLAGCAP] := gTime + 2000;
5159 with gFlags[FFlag] do
5160 begin
5161 Obj.X := FObj.X;
5162 Obj.Y := FObj.Y;
5163 Direction := FDirection;
5164 State := FLAG_STATE_DROPPED;
5165 Count := FLAG_TIME;
5166 g_Obj_Push(@Obj, (FObj.Vel.X div 2)-2+Random(5),
5167 (FObj.Vel.Y div 2)-2+Random(5));
5168 positionChanged(); // this updates spatial accelerators
5170 if FFlag = FLAG_RED then
5171 s := _lc[I_PLAYER_FLAG_RED]
5172 else
5173 s := _lc[I_PLAYER_FLAG_BLUE];
5175 g_Console_Add(Format(_lc[I_PLAYER_FLAG_DROP], [FName, s]), True);
5176 g_Game_Message(Format(_lc[I_MESSAGE_FLAG_DROP], [AnsiUpperCase(s)]), 144);
5178 if ((Self = gPlayer1) or (Self = gPlayer2)
5179 or ((gPlayer1 <> nil) and (gPlayer1.Team = FTeam))
5180 or ((gPlayer2 <> nil) and (gPlayer2.Team = FTeam))) then
5181 a := 0
5182 else
5183 a := 1;
5185 if (not Silent) and (not sound_lost_flag[a].IsPlaying()) then
5186 sound_lost_flag[a].Play();
5188 if g_Game_IsNet then
5189 MH_SEND_FlagEvent(FLAG_STATE_DROPPED, Flag, FUID, False);
5190 end;
5191 SetFlag(FLAG_NONE);
5192 Result := True;
5193 end;
5195 procedure TPlayer.GetSecret();
5196 begin
5197 if (self = gPlayer1) or (self = gPlayer2) then
5198 begin
5199 g_Console_Add(Format(_lc[I_PLAYER_SECRET], [FName]), True);
5200 g_Sound_PlayEx('SOUND_GAME_SECRET');
5201 end;
5202 Inc(FSecrets);
5203 end;
5205 procedure TPlayer.PressKey(Key: Byte; Time: Word = 1);
5206 begin
5207 Assert(Key <= High(FKeys));
5209 FKeys[Key].Pressed := True;
5210 FKeys[Key].Time := Time;
5211 end;
5213 function TPlayer.IsKeyPressed(K: Byte): Boolean;
5214 begin
5215 Result := FKeys[K].Pressed;
5216 end;
5218 procedure TPlayer.ReleaseKeys();
5219 var
5220 a: Integer;
5221 begin
5222 for a := Low(FKeys) to High(FKeys) do
5223 begin
5224 FKeys[a].Pressed := False;
5225 FKeys[a].Time := 0;
5226 end;
5227 end;
5229 procedure TPlayer.OnDamage(Angle: SmallInt);
5230 begin
5231 end;
5233 function TPlayer.firediry(): Integer;
5234 begin
5235 if FKeys[KEY_UP].Pressed then Result := -42
5236 else if FKeys[KEY_DOWN].Pressed then Result := 19
5237 else Result := 0;
5238 end;
5240 procedure TPlayer.RememberState();
5241 var
5242 i: Integer;
5243 SavedState: TPlayerSavedState;
5244 begin
5245 SavedState.Health := FHealth;
5246 SavedState.Armor := FArmor;
5247 SavedState.Air := FAir;
5248 SavedState.JetFuel := FJetFuel;
5249 SavedState.CurrWeap := FCurrWeap;
5250 SavedState.NextWeap := FNextWeap;
5251 SavedState.NextWeapDelay := FNextWeapDelay;
5252 for i := Low(FWeapon) to High(FWeapon) do
5253 SavedState.Weapon[i] := FWeapon[i];
5254 for i := Low(FAmmo) to High(FAmmo) do
5255 SavedState.Ammo[i] := FAmmo[i];
5256 for i := Low(FMaxAmmo) to High(FMaxAmmo) do
5257 SavedState.MaxAmmo[i] := FMaxAmmo[i];
5258 SavedState.Rulez := FRulez - [R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE];
5260 FSavedStateNum := -1;
5261 for i := Low(SavedStates) to High(SavedStates) do
5262 if not SavedStates[i].Used then
5263 begin
5264 FSavedStateNum := i;
5265 break;
5266 end;
5267 if FSavedStateNum < 0 then
5268 begin
5269 SetLength(SavedStates, Length(SavedStates) + 1);
5270 FSavedStateNum := High(SavedStates);
5271 end;
5273 SavedState.Used := True;
5274 SavedStates[FSavedStateNum] := SavedState;
5275 end;
5277 procedure TPlayer.RecallState();
5278 var
5279 i: Integer;
5280 SavedState: TPlayerSavedState;
5281 begin
5282 if(FSavedStateNum < 0) or (FSavedStateNum > High(SavedStates)) then
5283 Exit;
5285 SavedState := SavedStates[FSavedStateNum];
5286 SavedStates[FSavedStateNum].Used := False;
5287 FSavedStateNum := -1;
5289 FHealth := SavedState.Health;
5290 FArmor := SavedState.Armor;
5291 FAir := SavedState.Air;
5292 FJetFuel := SavedState.JetFuel;
5293 FCurrWeap := SavedState.CurrWeap;
5294 FNextWeap := SavedState.NextWeap;
5295 FNextWeapDelay := SavedState.NextWeapDelay;
5296 for i := Low(FWeapon) to High(FWeapon) do
5297 FWeapon[i] := SavedState.Weapon[i];
5298 for i := Low(FAmmo) to High(FAmmo) do
5299 FAmmo[i] := SavedState.Ammo[i];
5300 for i := Low(FMaxAmmo) to High(FMaxAmmo) do
5301 FMaxAmmo[i] := SavedState.MaxAmmo[i];
5302 FRulez := SavedState.Rulez;
5304 if gGameSettings.GameType = GT_SERVER then
5305 MH_SEND_PlayerStats(FUID);
5306 end;
5308 procedure TPlayer.SaveState (st: TStream);
5309 var
5310 i: Integer;
5311 b: Byte;
5312 begin
5313 // Сигнатура игрока
5314 utils.writeSign(st, 'PLYR');
5315 utils.writeInt(st, Byte(PLR_SAVE_VERSION)); // version
5316 // Бот или человек
5317 utils.writeBool(st, FIamBot);
5318 // UID игрока
5319 utils.writeInt(st, Word(FUID));
5320 // Имя игрока
5321 utils.writeStr(st, FName);
5322 // Команда
5323 utils.writeInt(st, Byte(FTeam));
5324 // Жив ли
5325 utils.writeBool(st, FAlive);
5326 // Израсходовал ли все жизни
5327 utils.writeBool(st, FNoRespawn);
5328 // Направление
5329 if FDirection = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT
5330 utils.writeInt(st, Byte(b));
5331 // Здоровье
5332 utils.writeInt(st, LongInt(FHealth));
5333 // Коэффициент инвалидности
5334 utils.writeInt(st, LongInt(FHandicap));
5335 // Жизни
5336 utils.writeInt(st, Byte(FLives));
5337 // Броня
5338 utils.writeInt(st, LongInt(FArmor));
5339 // Запас воздуха
5340 utils.writeInt(st, LongInt(FAir));
5341 // Запас горючего
5342 utils.writeInt(st, LongInt(FJetFuel));
5343 // Боль
5344 utils.writeInt(st, LongInt(FPain));
5345 // Убил
5346 utils.writeInt(st, LongInt(FKills));
5347 // Убил монстров
5348 utils.writeInt(st, LongInt(FMonsterKills));
5349 // Фрагов
5350 utils.writeInt(st, LongInt(FFrags));
5351 // Фрагов подряд
5352 utils.writeInt(st, Byte(FFragCombo));
5353 // Время последнего фрага
5354 utils.writeInt(st, LongWord(FLastFrag));
5355 // Смертей
5356 utils.writeInt(st, LongInt(FDeath));
5357 // Какой флаг несет
5358 utils.writeInt(st, Byte(FFlag));
5359 // Нашел секретов
5360 utils.writeInt(st, LongInt(FSecrets));
5361 // Текущее оружие
5362 utils.writeInt(st, Byte(FCurrWeap));
5363 // Желаемое оружие
5364 utils.writeInt(st, Word(FNextWeap));
5365 // ...и пауза
5366 utils.writeInt(st, Byte(FNextWeapDelay));
5367 // Время зарядки BFG
5368 utils.writeInt(st, SmallInt(FBFGFireCounter));
5369 // Буфер урона
5370 utils.writeInt(st, LongInt(FDamageBuffer));
5371 // Последний ударивший
5372 utils.writeInt(st, Word(FLastSpawnerUID));
5373 // Тип последнего полученного урона
5374 utils.writeInt(st, Byte(FLastHit));
5375 // Объект игрока
5376 Obj_SaveState(st, @FObj);
5377 // Текущее количество патронов
5378 for i := A_BULLETS to A_HIGH do utils.writeInt(st, Word(FAmmo[i]));
5379 // Максимальное количество патронов
5380 for i := A_BULLETS to A_HIGH do utils.writeInt(st, Word(FMaxAmmo[i]));
5381 // Наличие оружия
5382 for i := WP_FIRST to WP_LAST do utils.writeBool(st, FWeapon[i]);
5383 // Время перезарядки оружия
5384 for i := WP_FIRST to WP_LAST do utils.writeInt(st, Word(FReloading[i]));
5385 // Наличие рюкзака
5386 utils.writeBool(st, (R_ITEM_BACKPACK in FRulez));
5387 // Наличие красного ключа
5388 utils.writeBool(st, (R_KEY_RED in FRulez));
5389 // Наличие зеленого ключа
5390 utils.writeBool(st, (R_KEY_GREEN in FRulez));
5391 // Наличие синего ключа
5392 utils.writeBool(st, (R_KEY_BLUE in FRulez));
5393 // Наличие берсерка
5394 utils.writeBool(st, (R_BERSERK in FRulez));
5395 // Время действия специальных предметов
5396 for i := MR_SUIT to MR_MAX do utils.writeInt(st, LongWord(FMegaRulez[i]));
5397 // Время до повторного респауна, смены оружия, исользования, захвата флага
5398 for i := T_RESPAWN to T_FLAGCAP do utils.writeInt(st, LongWord(FTime[i]));
5399 // Название модели
5400 utils.writeStr(st, FModel.GetName());
5401 // Цвет модели
5402 utils.writeInt(st, Byte(FColor.R));
5403 utils.writeInt(st, Byte(FColor.G));
5404 utils.writeInt(st, Byte(FColor.B));
5405 end;
5408 procedure TPlayer.LoadState (st: TStream);
5409 var
5410 i: Integer;
5411 str: String;
5412 b: Byte;
5413 begin
5414 assert(st <> nil);
5416 // Сигнатура игрока
5417 if not utils.checkSign(st, 'PLYR') then raise XStreamError.Create('invalid player signature');
5418 if (utils.readByte(st) <> PLR_SAVE_VERSION) then raise XStreamError.Create('invalid player version');
5419 // Бот или человек:
5420 FIamBot := utils.readBool(st);
5421 // UID игрока
5422 FUID := utils.readWord(st);
5423 // Имя игрока
5424 str := utils.readStr(st);
5425 if (self <> gPlayer1) and (self <> gPlayer2) then FName := str;
5426 // Команда
5427 FTeam := utils.readByte(st);
5428 // Жив ли
5429 FAlive := utils.readBool(st);
5430 // Израсходовал ли все жизни
5431 FNoRespawn := utils.readBool(st);
5432 // Направление
5433 b := utils.readByte(st);
5434 if b = 1 then FDirection := TDirection.D_LEFT else FDirection := TDirection.D_RIGHT; // b = 2
5435 // Здоровье
5436 FHealth := utils.readLongInt(st);
5437 // Коэффициент инвалидности
5438 FHandicap := utils.readLongInt(st);
5439 // Жизни
5440 FLives := utils.readByte(st);
5441 // Броня
5442 FArmor := utils.readLongInt(st);
5443 // Запас воздуха
5444 FAir := utils.readLongInt(st);
5445 // Запас горючего
5446 FJetFuel := utils.readLongInt(st);
5447 // Боль
5448 FPain := utils.readLongInt(st);
5449 // Убил
5450 FKills := utils.readLongInt(st);
5451 // Убил монстров
5452 FMonsterKills := utils.readLongInt(st);
5453 // Фрагов
5454 FFrags := utils.readLongInt(st);
5455 // Фрагов подряд
5456 FFragCombo := utils.readByte(st);
5457 // Время последнего фрага
5458 FLastFrag := utils.readLongWord(st);
5459 // Смертей
5460 FDeath := utils.readLongInt(st);
5461 // Какой флаг несет
5462 FFlag := utils.readByte(st);
5463 // Нашел секретов
5464 FSecrets := utils.readLongInt(st);
5465 // Текущее оружие
5466 FCurrWeap := utils.readByte(st);
5467 // Желаемое оружие
5468 FNextWeap := utils.readWord(st);
5469 // ...и пауза
5470 FNextWeapDelay := utils.readByte(st);
5471 // Время зарядки BFG
5472 FBFGFireCounter := utils.readSmallInt(st);
5473 // Буфер урона
5474 FDamageBuffer := utils.readLongInt(st);
5475 // Последний ударивший
5476 FLastSpawnerUID := utils.readWord(st);
5477 // Тип последнего полученного урона
5478 FLastHit := utils.readByte(st);
5479 // Объект игрока
5480 Obj_LoadState(@FObj, st);
5481 // Текущее количество патронов
5482 for i := A_BULLETS to A_HIGH do FAmmo[i] := utils.readWord(st);
5483 // Максимальное количество патронов
5484 for i := A_BULLETS to A_HIGH do FMaxAmmo[i] := utils.readWord(st);
5485 // Наличие оружия
5486 for i := WP_FIRST to WP_LAST do FWeapon[i] := utils.readBool(st);
5487 // Время перезарядки оружия
5488 for i := WP_FIRST to WP_LAST do FReloading[i] := utils.readWord(st);
5489 // Наличие рюкзака
5490 if utils.readBool(st) then Include(FRulez, R_ITEM_BACKPACK);
5491 // Наличие красного ключа
5492 if utils.readBool(st) then Include(FRulez, R_KEY_RED);
5493 // Наличие зеленого ключа
5494 if utils.readBool(st) then Include(FRulez, R_KEY_GREEN);
5495 // Наличие синего ключа
5496 if utils.readBool(st) then Include(FRulez, R_KEY_BLUE);
5497 // Наличие берсерка
5498 if utils.readBool(st) then Include(FRulez, R_BERSERK);
5499 // Время действия специальных предметов
5500 for i := MR_SUIT to MR_MAX do FMegaRulez[i] := utils.readLongWord(st);
5501 // Время до повторного респауна, смены оружия, исользования, захвата флага
5502 for i := T_RESPAWN to T_FLAGCAP do FTime[i] := utils.readLongWord(st);
5503 // Название модели
5504 str := utils.readStr(st);
5505 // Цвет модели
5506 FColor.R := utils.readByte(st);
5507 FColor.G := utils.readByte(st);
5508 FColor.B := utils.readByte(st);
5509 if (self = gPlayer1) then
5510 begin
5511 str := gPlayer1Settings.Model;
5512 FColor := gPlayer1Settings.Color;
5513 end
5514 else if (self = gPlayer2) then
5515 begin
5516 str := gPlayer2Settings.Model;
5517 FColor := gPlayer2Settings.Color;
5518 end;
5519 // Обновляем модель игрока
5520 SetModel(str);
5521 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
5522 FModel.Color := TEAMCOLOR[FTeam]
5523 else
5524 FModel.Color := FColor;
5525 end;
5528 procedure TPlayer.AllRulez(Health: Boolean);
5529 var
5530 a: Integer;
5531 begin
5532 if Health then
5533 begin
5534 FHealth := PLAYER_HP_LIMIT;
5535 FArmor := PLAYER_AP_LIMIT;
5536 Exit;
5537 end;
5539 for a := WP_FIRST to WP_LAST do FWeapon[a] := True;
5540 for a := A_BULLETS to A_HIGH do FAmmo[a] := 30000;
5541 FRulez := FRulez+[R_KEY_RED, R_KEY_GREEN, R_KEY_BLUE];
5542 end;
5544 procedure TPlayer.RestoreHealthArmor();
5545 begin
5546 FHealth := PLAYER_HP_LIMIT;
5547 FArmor := PLAYER_AP_LIMIT;
5548 end;
5550 procedure TPlayer.FragCombo();
5551 var
5552 Param: Integer;
5553 begin
5554 if (gGameSettings.GameMode in [GM_COOP, GM_SINGLE]) or g_Game_IsClient then
5555 Exit;
5556 if gTime - FLastFrag < FRAG_COMBO_TIME then
5557 begin
5558 if FFragCombo < 5 then
5559 Inc(FFragCombo);
5560 Param := FUID or (FFragCombo shl 16);
5561 if (FComboEvnt >= Low(gDelayedEvents)) and
5562 (FComboEvnt <= High(gDelayedEvents)) and
5563 gDelayedEvents[FComboEvnt].Pending and
5564 (gDelayedEvents[FComboEvnt].DEType = DE_KILLCOMBO) and
5565 (gDelayedEvents[FComboEvnt].DENum and $FFFF = FUID) then
5566 begin
5567 gDelayedEvents[FComboEvnt].Time := gTime + 500;
5568 gDelayedEvents[FComboEvnt].DENum := Param;
5569 end
5570 else
5571 FComboEvnt := g_Game_DelayEvent(DE_KILLCOMBO, 500, Param);
5572 end
5573 else
5574 FFragCombo := 1;
5576 FLastFrag := gTime;
5577 end;
5579 procedure TPlayer.GiveItem(ItemType: Byte);
5580 begin
5581 case ItemType of
5582 ITEM_SUIT:
5583 if FMegaRulez[MR_SUIT] < gTime+PLAYER_SUIT_TIME then
5584 begin
5585 FMegaRulez[MR_SUIT] := gTime+PLAYER_SUIT_TIME;
5586 end;
5588 ITEM_OXYGEN:
5589 if FAir < AIR_MAX then
5590 begin
5591 FAir := AIR_MAX;
5592 end;
5594 ITEM_MEDKIT_BLACK:
5595 begin
5596 if not (R_BERSERK in FRulez) then
5597 begin
5598 Include(FRulez, R_BERSERK);
5599 if FBFGFireCounter < 1 then
5600 begin
5601 FCurrWeap := WEAPON_KASTET;
5602 resetWeaponQueue();
5603 FModel.SetWeapon(WEAPON_KASTET);
5604 end;
5605 if gFlash <> 0 then
5606 Inc(FPain, 100);
5607 FBerserk := gTime+30000;
5608 end;
5609 if FHealth < PLAYER_HP_SOFT then
5610 begin
5611 FHealth := PLAYER_HP_SOFT;
5612 FBerserk := gTime+30000;
5613 end;
5614 end;
5616 ITEM_INVUL:
5617 if FMegaRulez[MR_INVUL] < gTime+PLAYER_INVUL_TIME then
5618 begin
5619 FMegaRulez[MR_INVUL] := gTime+PLAYER_INVUL_TIME;
5620 FSpawnInvul := 0;
5621 end;
5623 ITEM_INVIS:
5624 if FMegaRulez[MR_INVIS] < gTime+PLAYER_INVIS_TIME then
5625 begin
5626 FMegaRulez[MR_INVIS] := gTime+PLAYER_INVIS_TIME;
5627 end;
5629 ITEM_JETPACK:
5630 if FJetFuel < JET_MAX then
5631 begin
5632 FJetFuel := JET_MAX;
5633 end;
5635 ITEM_MEDKIT_SMALL: if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 10, PLAYER_HP_SOFT);
5636 ITEM_MEDKIT_LARGE: if FHealth < PLAYER_HP_SOFT then IncMax(FHealth, 25, PLAYER_HP_SOFT);
5638 ITEM_ARMOR_GREEN: if FArmor < PLAYER_AP_SOFT then FArmor := PLAYER_AP_SOFT;
5639 ITEM_ARMOR_BLUE: if FArmor < PLAYER_AP_LIMIT then FArmor := PLAYER_AP_LIMIT;
5641 ITEM_SPHERE_BLUE: if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 100, PLAYER_HP_LIMIT);
5642 ITEM_SPHERE_WHITE:
5643 if (FHealth < PLAYER_HP_LIMIT) or (FArmor < PLAYER_AP_LIMIT) then
5644 begin
5645 if FHealth < PLAYER_HP_LIMIT then FHealth := PLAYER_HP_LIMIT;
5646 if FArmor < PLAYER_AP_LIMIT then FArmor := PLAYER_AP_LIMIT;
5647 end;
5649 ITEM_WEAPON_SAW: FWeapon[WEAPON_SAW] := True;
5650 ITEM_WEAPON_SHOTGUN1: FWeapon[WEAPON_SHOTGUN1] := True;
5651 ITEM_WEAPON_SHOTGUN2: FWeapon[WEAPON_SHOTGUN2] := True;
5652 ITEM_WEAPON_CHAINGUN: FWeapon[WEAPON_CHAINGUN] := True;
5653 ITEM_WEAPON_ROCKETLAUNCHER: FWeapon[WEAPON_ROCKETLAUNCHER] := True;
5654 ITEM_WEAPON_PLASMA: FWeapon[WEAPON_PLASMA] := True;
5655 ITEM_WEAPON_BFG: FWeapon[WEAPON_BFG] := True;
5656 ITEM_WEAPON_SUPERPULEMET: FWeapon[WEAPON_SUPERPULEMET] := True;
5657 ITEM_WEAPON_FLAMETHROWER: FWeapon[WEAPON_FLAMETHROWER] := True;
5659 ITEM_AMMO_BULLETS: if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
5660 ITEM_AMMO_BULLETS_BOX: if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then IncMax(FAmmo[A_BULLETS], 50, FMaxAmmo[A_BULLETS]);
5661 ITEM_AMMO_SHELLS: if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
5662 ITEM_AMMO_SHELLS_BOX: if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then IncMax(FAmmo[A_SHELLS], 25, FMaxAmmo[A_SHELLS]);
5663 ITEM_AMMO_ROCKET: if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
5664 ITEM_AMMO_ROCKET_BOX: if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then IncMax(FAmmo[A_ROCKETS], 5, FMaxAmmo[A_ROCKETS]);
5665 ITEM_AMMO_CELL: if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
5666 ITEM_AMMO_CELL_BIG: if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then IncMax(FAmmo[A_CELLS], 100, FMaxAmmo[A_CELLS]);
5667 ITEM_AMMO_FUELCAN: if FAmmo[A_FUEL] < FMaxAmmo[A_FUEL] then IncMax(FAmmo[A_FUEL], 100, FMaxAmmo[A_FUEL]);
5669 ITEM_AMMO_BACKPACK:
5670 if (FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS]) or
5671 (FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS]) or
5672 (FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS]) or
5673 (FAmmo[A_CELLS] < FMaxAmmo[A_CELLS]) or
5674 (FMaxAmmo[A_FUEL] < AmmoLimits[1, A_FUEL]) then
5675 begin
5676 FMaxAmmo[A_BULLETS] := AmmoLimits[1, A_BULLETS];
5677 FMaxAmmo[A_SHELLS] := AmmoLimits[1, A_SHELLS];
5678 FMaxAmmo[A_ROCKETS] := AmmoLimits[1, A_ROCKETS];
5679 FMaxAmmo[A_CELLS] := AmmoLimits[1, A_CELLS];
5680 FMaxAmmo[A_FUEL] := AmmoLimits[1, A_FUEL];
5682 if FAmmo[A_BULLETS] < FMaxAmmo[A_BULLETS] then IncMax(FAmmo[A_BULLETS], 10, FMaxAmmo[A_BULLETS]);
5683 if FAmmo[A_SHELLS] < FMaxAmmo[A_SHELLS] then IncMax(FAmmo[A_SHELLS], 4, FMaxAmmo[A_SHELLS]);
5684 if FAmmo[A_ROCKETS] < FMaxAmmo[A_ROCKETS] then IncMax(FAmmo[A_ROCKETS], 1, FMaxAmmo[A_ROCKETS]);
5685 if FAmmo[A_CELLS] < FMaxAmmo[A_CELLS] then IncMax(FAmmo[A_CELLS], 40, FMaxAmmo[A_CELLS]);
5687 FRulez := FRulez + [R_ITEM_BACKPACK];
5688 end;
5690 ITEM_KEY_RED: if not (R_KEY_RED in FRulez) then Include(FRulez, R_KEY_RED);
5691 ITEM_KEY_GREEN: if not (R_KEY_GREEN in FRulez) then Include(FRulez, R_KEY_GREEN);
5692 ITEM_KEY_BLUE: if not (R_KEY_BLUE in FRulez) then Include(FRulez, R_KEY_BLUE);
5694 ITEM_BOTTLE: if FHealth < PLAYER_HP_LIMIT then IncMax(FHealth, 4, PLAYER_HP_LIMIT);
5695 ITEM_HELMET: if FArmor < PLAYER_AP_LIMIT then IncMax(FArmor, 5, PLAYER_AP_LIMIT);
5697 else
5698 Exit;
5699 end;
5700 if g_Game_IsNet and g_Game_IsServer then
5701 MH_SEND_PlayerStats(FUID);
5702 end;
5704 procedure TPlayer.FlySmoke(Times: DWORD = 1);
5705 var i: DWORD;
5706 begin
5707 if (Random(5) = 1) and (Times = 1) then
5708 Exit;
5710 if BodyInLiquid(0, 0) then
5711 begin
5712 {$IFDEF ENABLE_GFX}
5713 g_GFX_Bubbles(Obj.X+Obj.Rect.X+(Obj.Rect.Width div 2)+Random(3)-1,
5714 Obj.Y+Obj.Rect.Height+8, 1, 8, 4);
5715 {$ENDIF}
5716 if Random(2) = 0 then
5717 g_Sound_PlayExAt('SOUND_GAME_BUBBLE1', FObj.X, FObj.Y)
5718 else
5719 g_Sound_PlayExAt('SOUND_GAME_BUBBLE2', FObj.X, FObj.Y);
5720 Exit;
5721 end;
5723 for i := 1 to Times do
5724 begin
5725 {$IFDEF ENABLE_GFX}
5726 g_GFX_QueueEffect(
5727 R_GFX_SMOKE_TRANS,
5728 Obj.X+Obj.Rect.X+Random(Obj.Rect.Width+Times*2)-(R_GFX_SMOKE_WIDTH div 2),
5729 Obj.Y+Obj.Rect.Height-4+Random(8+Times*2)
5730 );
5731 {$ENDIF}
5732 end;
5733 end;
5735 procedure TPlayer.OnFireFlame(Times: DWORD = 1);
5736 var i: DWORD;
5737 begin
5738 if (Random(10) = 1) and (Times = 1) then
5739 Exit;
5741 for i := 1 to Times do
5742 begin
5743 {$IFDEF ENABLE_GFX}
5744 g_GFX_QueueEffect(
5745 R_GFX_FLAME,
5746 Obj.X+Obj.Rect.X+Random(Obj.Rect.Width+Times*2)-(R_GFX_FLAME_WIDTH div 2),
5747 Obj.Y+8+Random(8+Times*2)
5748 );
5749 {$ENDIF}
5750 end;
5751 end;
5753 procedure TPlayer.PauseSounds(Enable: Boolean);
5754 begin
5755 FSawSound.Pause(Enable);
5756 FSawSoundIdle.Pause(Enable);
5757 FSawSoundHit.Pause(Enable);
5758 FSawSoundSelect.Pause(Enable);
5759 FFlameSoundOn.Pause(Enable);
5760 FFlameSoundOff.Pause(Enable);
5761 FFlameSoundWork.Pause(Enable);
5762 FJetSoundFly.Pause(Enable);
5763 FJetSoundOn.Pause(Enable);
5764 FJetSoundOff.Pause(Enable);
5765 end;
5767 { T C o r p s e : }
5769 constructor TCorpse.Create(X, Y: Integer; ModelName: String; aMess: Boolean);
5770 begin
5771 g_Obj_Init(@FObj);
5772 FObj.X := X;
5773 FObj.Y := Y;
5774 FObj.Rect := PLAYER_CORPSERECT;
5775 FMess := aMess;
5776 FModel := g_PlayerModel_Get(ModelName);
5778 if FMess then
5779 begin
5780 FState := CORPSE_STATE_MESS;
5781 FModel.ChangeAnimation(A_DIE2);
5782 end
5783 else
5784 begin
5785 FState := CORPSE_STATE_NORMAL;
5786 FModel.ChangeAnimation(A_DIE1);
5787 end;
5788 end;
5790 destructor TCorpse.Destroy();
5791 begin
5792 FModel.Free;
5793 inherited;
5794 end;
5796 function TCorpse.ObjPtr (): PObj; inline; begin result := @FObj; end;
5798 procedure TCorpse.positionChanged (); inline; begin end;
5800 procedure TCorpse.moveBy (dx, dy: Integer); inline;
5801 begin
5802 if (dx <> 0) or (dy <> 0) then
5803 begin
5804 FObj.X += dx;
5805 FObj.Y += dy;
5806 positionChanged();
5807 end;
5808 end;
5811 procedure TCorpse.getMapBox (out x, y, w, h: Integer); inline;
5812 begin
5813 x := FObj.X+PLAYER_CORPSERECT.X;
5814 y := FObj.Y+PLAYER_CORPSERECT.Y;
5815 w := PLAYER_CORPSERECT.Width;
5816 h := PLAYER_CORPSERECT.Height;
5817 end;
5820 procedure TCorpse.Damage(Value: Word; SpawnerUID: Word; vx, vy: Integer);
5821 {$IFDEF ENABLE_GFX}
5822 var Blood: TModelBlood;
5823 {$ENDIF}
5824 begin
5825 if FState = CORPSE_STATE_REMOVEME then
5826 Exit;
5828 FDamage := FDamage + Value;
5830 if FDamage > 150 then
5831 begin
5832 if FModel <> nil then
5833 begin
5834 FState := CORPSE_STATE_REMOVEME;
5836 g_Player_CreateGibs(
5837 FObj.X + FObj.Rect.X + (FObj.Rect.Width div 2),
5838 FObj.Y + FObj.Rect.Y + (FObj.Rect.Height div 2),
5839 FModel.id,
5840 FModel.Color
5841 );
5843 // Звук мяса от трупа:
5844 FModel.PlaySound(MODELSOUND_DIE, 5, FObj.X, FObj.Y);
5846 // Зловещий смех:
5847 if (gBodyKillEvent <> -1) and gDelayedEvents[gBodyKillEvent].Pending then
5848 gDelayedEvents[gBodyKillEvent].Pending := False;
5849 gBodyKillEvent := g_Game_DelayEvent(DE_BODYKILL, 1050, SpawnerUID);
5851 FModel.Free;
5852 FModel := nil;
5853 end
5854 end
5855 else
5856 begin
5857 FObj.Vel.X := FObj.Vel.X + vx;
5858 FObj.Vel.Y := FObj.Vel.Y + vy;
5859 {$IFDEF ENABLE_GFX}
5860 Blood := FModel.GetBlood();
5861 g_GFX_Blood(FObj.X+PLAYER_CORPSERECT.X+(PLAYER_CORPSERECT.Width div 2),
5862 FObj.Y+PLAYER_CORPSERECT.Y+(PLAYER_CORPSERECT.Height div 2),
5863 Value, vx, vy, 16, (PLAYER_CORPSERECT.Height*2) div 3,
5864 Blood.R, Blood.G, Blood.B, Blood.Kind);
5865 {$ENDIF}
5866 end;
5867 end;
5869 procedure TCorpse.Update();
5870 var
5871 st: Word;
5872 begin
5873 if FState = CORPSE_STATE_REMOVEME then
5874 Exit;
5876 FObj.oldX := FObj.X;
5877 FObj.oldY := FObj.Y;
5879 if gTime mod (GAME_TICK*2) <> 0 then
5880 begin
5881 g_Obj_Move(@FObj, True, True, True);
5882 positionChanged(); // this updates spatial accelerators
5883 Exit;
5884 end;
5886 // Сопротивление воздуха для трупа:
5887 FObj.Vel.X := z_dec(FObj.Vel.X, 1);
5889 st := g_Obj_Move(@FObj, True, True, True);
5890 positionChanged(); // this updates spatial accelerators
5892 if WordBool(st and MOVE_FALLOUT) then
5893 begin
5894 FState := CORPSE_STATE_REMOVEME;
5895 Exit;
5896 end;
5898 if FModel <> nil then
5899 FModel.Update;
5900 end;
5903 procedure TCorpse.SaveState (st: TStream);
5904 var anim: Boolean;
5905 begin
5906 assert(st <> nil);
5908 // Сигнатура трупа
5909 utils.writeSign(st, 'CORP');
5910 utils.writeInt(st, Byte(0));
5911 // Состояние
5912 utils.writeInt(st, Byte(FState));
5913 // Накопленный урон
5914 utils.writeInt(st, Byte(FDamage));
5915 // Цвет
5916 utils.writeInt(st, Byte(FModel.Color.R));
5917 utils.writeInt(st, Byte(FModel.Color.G));
5918 utils.writeInt(st, Byte(FModel.Color.B));
5919 // Объект трупа
5920 Obj_SaveState(st, @FObj);
5921 utils.writeInt(st, Word(FPlayerUID));
5922 // animation
5923 anim := (FModel <> nil);
5924 utils.writeBool(st, anim);
5925 if anim then FModel.AnimState.SaveState(st, 0, False);
5926 // animation for mask (same as animation, compat with older saves)
5927 anim := (FModel <> nil);
5928 utils.writeBool(st, anim);
5929 if anim then FModel.AnimState.SaveState(st, 0, False);
5930 end;
5933 procedure TCorpse.LoadState (st: TStream);
5934 var anim, blending: Boolean; r, g, b, alpha: Byte; stub: TAnimationState;
5935 begin
5936 assert(st <> nil);
5938 // Сигнатура трупа
5939 if not utils.checkSign(st, 'CORP') then raise XStreamError.Create('invalid corpse signature');
5940 if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid corpse version');
5941 // Состояние
5942 FState := utils.readByte(st);
5943 // Накопленный урон
5944 FDamage := utils.readByte(st);
5945 // Цвет
5946 r := utils.readByte(st);
5947 g := utils.readByte(st);
5948 b := utils.readByte(st);
5949 FModel.SetColor(r, g, b);
5950 // Объект трупа
5951 Obj_LoadState(@FObj, st);
5952 FPlayerUID := utils.readWord(st);
5953 // animation
5954 stub := TAnimationState.Create(False, 0, 0);
5955 anim := utils.readBool(st);
5956 if anim then
5957 begin
5958 stub.LoadState(st, alpha, blending);
5959 FModel.AnimState.CurrentFrame := Min(stub.CurrentFrame, FModel.AnimState.Length);
5960 end
5961 else
5962 begin
5963 FModel.Free;
5964 FModel := nil
5965 end;
5966 // animation for mask (same as animation, compat with older saves)
5967 anim := utils.readBool(st);
5968 if anim then stub.LoadState(st, alpha, blending);
5969 stub.Free;
5970 end;
5972 { T B o t : }
5974 constructor TBot.Create();
5975 var
5976 a: Integer;
5977 begin
5978 inherited Create();
5980 FPhysics := True;
5981 FSpectator := False;
5982 FGhost := False;
5984 FIamBot := True;
5986 Inc(gNumBots);
5988 for a := WP_FIRST to WP_LAST do
5989 begin
5990 FDifficult.WeaponPrior[a] := WEAPON_PRIOR1[a];
5991 FDifficult.CloseWeaponPrior[a] := WEAPON_PRIOR2[a];
5992 //FDifficult.SafeWeaponPrior[a] := WEAPON_PRIOR3[a];
5993 end;
5994 end;
5996 destructor TBot.Destroy();
5997 begin
5998 Dec(gNumBots);
5999 inherited Destroy();
6000 end;
6002 procedure TBot.Respawn(Silent: Boolean; Force: Boolean = False);
6003 begin
6004 inherited Respawn(Silent, Force);
6006 FAIFlags := nil;
6007 FSelectedWeapon := FCurrWeap;
6008 resetWeaponQueue();
6009 FTargetUID := 0;
6010 end;
6012 procedure TBot.UpdateCombat();
6013 type
6014 TTarget = record
6015 UID: Word;
6016 X, Y: Integer;
6017 Rect: TRectWH;
6018 cX, cY: Integer;
6019 Dist: Word;
6020 Line: Boolean;
6021 Visible: Boolean;
6022 IsPlayer: Boolean;
6023 end;
6025 TTargetRecord = array of TTarget;
6027 function Compare(a, b: TTarget): Integer;
6028 begin
6029 if a.Line and not b.Line then // A на линии огня
6030 Result := -1
6031 else
6032 if not a.Line and b.Line then // B на линии огня
6033 Result := 1
6034 else // И A, и B на линии или не на линии огня
6035 if (a.Line and b.Line) or ((not a.Line) and (not b.Line)) then
6036 begin
6037 if a.Dist > b.Dist then // B ближе
6038 Result := 1
6039 else // A ближе или равноудаленно с B
6040 Result := -1;
6041 end
6042 else // Странно -> A
6043 Result := -1;
6044 end;
6046 var
6047 a, x1, y1, x2, y2: Integer;
6048 targets: TTargetRecord;
6049 ammo: Word;
6050 Target, BestTarget: TTarget;
6051 firew, fireh: Integer;
6052 angle: SmallInt;
6053 mon: TMonster;
6054 pla, tpla: TPlayer;
6055 vsPlayer, vsMonster, ok: Boolean;
6058 function monsUpdate (mon: TMonster): Boolean;
6059 begin
6060 result := false; // don't stop
6061 if mon.alive and (mon.MonsterType <> MONSTER_BARREL) then
6062 begin
6063 if not TargetOnScreen(mon.Obj.X+mon.Obj.Rect.X, mon.Obj.Y+mon.Obj.Rect.Y) then exit;
6065 x2 := mon.Obj.X+mon.Obj.Rect.X+(mon.Obj.Rect.Width div 2);
6066 y2 := mon.Obj.Y+mon.Obj.Rect.Y+(mon.Obj.Rect.Height div 2);
6068 // Если монстр на экране и не прикрыт стеной
6069 if g_TraceVector(x1, y1, x2, y2) then
6070 begin
6071 // Добавляем к списку возможных целей
6072 SetLength(targets, Length(targets)+1);
6073 with targets[High(targets)] do
6074 begin
6075 UID := mon.UID;
6076 X := mon.Obj.X;
6077 Y := mon.Obj.Y;
6078 cX := x2;
6079 cY := y2;
6080 Rect := mon.Obj.Rect;
6081 Dist := g_PatchLength(x1, y1, x2, y2);
6082 Line := (y1+4 < Target.Y + mon.Obj.Rect.Y + mon.Obj.Rect.Height) and
6083 (y1-4 > Target.Y + mon.Obj.Rect.Y);
6084 Visible := True;
6085 IsPlayer := False;
6086 end;
6087 end;
6088 end;
6089 end;
6091 begin
6092 vsPlayer := LongBool(gGameSettings.Options and GAME_OPTION_BOTVSPLAYER);
6093 vsMonster := LongBool(gGameSettings.Options and GAME_OPTION_BOTVSMONSTER);
6095 // Если текущее оружие не то, что нужно, то меняем:
6096 if FCurrWeap <> FSelectedWeapon then
6097 NextWeapon();
6099 // Если нужно стрелять и нужное оружие, то нажать "Стрелять":
6100 if (GetAIFlag('NEEDFIRE') <> '') and (FCurrWeap = FSelectedWeapon) then
6101 begin
6102 RemoveAIFlag('NEEDFIRE');
6104 case FCurrWeap of
6105 WEAPON_PLASMA, WEAPON_SUPERPULEMET, WEAPON_CHAINGUN: PressKey(KEY_FIRE, 20);
6106 WEAPON_SAW, WEAPON_KASTET, WEAPON_FLAMETHROWER: PressKey(KEY_FIRE, 40);
6107 else PressKey(KEY_FIRE);
6108 end;
6109 end;
6111 // Координаты ствола:
6112 x1 := FObj.X + WEAPONPOINT[FDirection].X;
6113 y1 := FObj.Y + WEAPONPOINT[FDirection].Y;
6115 Target.UID := FTargetUID;
6117 ok := False;
6118 if Target.UID <> 0 then
6119 begin // Цель есть - настраиваем
6120 if (g_GetUIDType(Target.UID) = UID_PLAYER) and
6121 vsPlayer then
6122 begin // Игрок
6123 tpla := g_Player_Get(Target.UID);
6124 if tpla <> nil then
6125 with tpla do
6126 begin
6127 if (@FObj) <> nil then
6128 begin
6129 Target.X := FObj.X;
6130 Target.Y := FObj.Y;
6131 end;
6132 end;
6134 Target.cX := Target.X + PLAYER_RECT_CX;
6135 Target.cY := Target.Y + PLAYER_RECT_CY;
6136 Target.Rect := PLAYER_RECT;
6137 Target.Visible := g_TraceVector(x1, y1, Target.cX, Target.cY);
6138 Target.Line := (y1+4 < Target.Y+PLAYER_RECT.Y+PLAYER_RECT.Height) and
6139 (y1-4 > Target.Y+PLAYER_RECT.Y);
6140 Target.IsPlayer := True;
6141 ok := True;
6142 end
6143 else
6144 if (g_GetUIDType(Target.UID) = UID_MONSTER) and
6145 vsMonster then
6146 begin // Монстр
6147 mon := g_Monsters_ByUID(Target.UID);
6148 if mon <> nil then
6149 begin
6150 Target.X := mon.Obj.X;
6151 Target.Y := mon.Obj.Y;
6153 Target.cX := Target.X + mon.Obj.Rect.X + (mon.Obj.Rect.Width div 2);
6154 Target.cY := Target.Y + mon.Obj.Rect.Y + (mon.Obj.Rect.Height div 2);
6155 Target.Rect := mon.Obj.Rect;
6156 Target.Visible := g_TraceVector(x1, y1, Target.cX, Target.cY);
6157 Target.Line := (y1+4 < Target.Y + mon.Obj.Rect.Y + mon.Obj.Rect.Height) and
6158 (y1-4 > Target.Y + mon.Obj.Rect.Y);
6159 Target.IsPlayer := False;
6160 ok := True;
6161 end;
6162 end;
6163 end;
6165 if not ok then
6166 begin // Цели нет - обнуляем
6167 Target.X := 0;
6168 Target.Y := 0;
6169 Target.cX := 0;
6170 Target.cY := 0;
6171 Target.Visible := False;
6172 Target.Line := False;
6173 Target.IsPlayer := False;
6174 end;
6176 targets := nil;
6178 // Если цель не видима или не на линии огня, то ищем все возможные цели:
6179 if (not Target.Line) or (not Target.Visible) then
6180 begin
6181 // Игроки:
6182 if vsPlayer then
6183 for a := 0 to High(gPlayers) do
6184 if (gPlayers[a] <> nil) and (gPlayers[a].alive) and
6185 (gPlayers[a].FUID <> FUID) and
6186 (not SameTeam(FUID, gPlayers[a].FUID)) and
6187 (not gPlayers[a].NoTarget) and
6188 (gPlayers[a].FMegaRulez[MR_INVIS] < gTime) then
6189 begin
6190 if not TargetOnScreen(gPlayers[a].FObj.X + PLAYER_RECT.X,
6191 gPlayers[a].FObj.Y + PLAYER_RECT.Y) then
6192 Continue;
6194 x2 := gPlayers[a].FObj.X + PLAYER_RECT_CX;
6195 y2 := gPlayers[a].FObj.Y + PLAYER_RECT_CY;
6197 // Если игрок на экране и не прикрыт стеной:
6198 if g_TraceVector(x1, y1, x2, y2) then
6199 begin
6200 // Добавляем к списку возможных целей:
6201 SetLength(targets, Length(targets)+1);
6202 with targets[High(targets)] do
6203 begin
6204 UID := gPlayers[a].FUID;
6205 X := gPlayers[a].FObj.X;
6206 Y := gPlayers[a].FObj.Y;
6207 cX := x2;
6208 cY := y2;
6209 Rect := PLAYER_RECT;
6210 Dist := g_PatchLength(x1, y1, x2, y2);
6211 Line := (y1+4 < Target.Y+PLAYER_RECT.Y+PLAYER_RECT.Height) and
6212 (y1-4 > Target.Y+PLAYER_RECT.Y);
6213 Visible := True;
6214 IsPlayer := True;
6215 end;
6216 end;
6217 end;
6219 // Монстры:
6220 if vsMonster then g_Mons_ForEach(monsUpdate);
6221 end;
6223 // Если есть возможные цели:
6224 // (Выбираем лучшую, меняем оружие и бежим к ней/от нее)
6225 if targets <> nil then
6226 begin
6227 // Выбираем наилучшую цель:
6228 BestTarget := targets[0];
6229 if Length(targets) > 1 then
6230 for a := 1 to High(targets) do
6231 if Compare(BestTarget, targets[a]) = 1 then
6232 BestTarget := targets[a];
6234 // Если лучшая цель "виднее" текущей, то текущая := лучшая:
6235 if ((not Target.Visible) and BestTarget.Visible and (Target.UID <> BestTarget.UID)) or
6236 ((not Target.Line) and BestTarget.Line and BestTarget.Visible) then
6237 begin
6238 Target := BestTarget;
6240 if (Healthy() = 3) or ((Healthy() = 2)) then
6241 begin // Если здоровы - догоняем
6242 if ((RunDirection() = TDirection.D_LEFT) and (Target.X > FObj.X)) then
6243 SetAIFlag('GORIGHT', '1');
6244 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X < FObj.X)) then
6245 SetAIFlag('GOLEFT', '1');
6246 end
6247 else
6248 begin // Если побиты - убегаем
6249 if ((RunDirection() = TDirection.D_LEFT) and (Target.X < FObj.X)) then
6250 SetAIFlag('GORIGHT', '1');
6251 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X > FObj.X)) then
6252 SetAIFlag('GOLEFT', '1');
6253 end;
6255 // Выбираем оружие на основе расстояния и приоритетов:
6256 SelectWeapon(Abs(x1-Target.cX));
6257 end;
6258 end;
6260 // Если есть цель:
6261 // (Догоняем/убегаем, стреляем по направлению к цели)
6262 // (Если цель далеко, то хватит следить за ней)
6263 if Target.UID <> 0 then
6264 begin
6265 if not TargetOnScreen(Target.X + Target.Rect.X,
6266 Target.Y + Target.Rect.Y) then
6267 begin // Цель сбежала с "экрана"
6268 if (Healthy() = 3) or ((Healthy() = 2)) then
6269 begin // Если здоровы - догоняем
6270 if ((RunDirection() = TDirection.D_LEFT) and (Target.X > FObj.X)) then
6271 SetAIFlag('GORIGHT', '1');
6272 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X < FObj.X)) then
6273 SetAIFlag('GOLEFT', '1');
6274 end
6275 else
6276 begin // Если побиты - забываем о цели и убегаем
6277 Target.UID := 0;
6278 if ((RunDirection() = TDirection.D_LEFT) and (Target.X < FObj.X)) then
6279 SetAIFlag('GORIGHT', '1');
6280 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X > FObj.X)) then
6281 SetAIFlag('GOLEFT', '1');
6282 end;
6283 end
6284 else
6285 begin // Цель пока на "экране"
6286 // Если цель не загорожена стеной, то отмечаем, когда ее видели:
6287 if g_TraceVector(x1, y1, Target.cX, Target.cY) then
6288 FLastVisible := gTime;
6289 // Если разница высот не велика, то догоняем:
6290 if (Abs(FObj.Y-Target.Y) <= 128) then
6291 begin
6292 if ((RunDirection() = TDirection.D_LEFT) and (Target.X > FObj.X)) then
6293 SetAIFlag('GORIGHT', '1');
6294 if ((RunDirection() = TDirection.D_RIGHT) and (Target.X < FObj.X)) then
6295 SetAIFlag('GOLEFT', '1');
6296 end;
6297 end;
6299 // Выбираем угол вверх:
6300 if FDirection = TDirection.D_LEFT then
6301 angle := ANGLE_LEFTUP
6302 else
6303 angle := ANGLE_RIGHTUP;
6305 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6306 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6308 // Если при угле вверх можно попасть в приблизительное положение цели:
6309 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6310 Target.X+Target.Rect.X+GetInterval(FDifficult.DiagPrecision, 128), //96
6311 Target.Y+Target.Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6312 Target.Rect.Width, Target.Rect.Height) and
6313 g_TraceVector(x1, y1, Target.cX, Target.cY) then
6314 begin // то нужно стрелять вверх
6315 SetAIFlag('NEEDFIRE', '1');
6316 SetAIFlag('NEEDSEEUP', '1');
6317 end;
6319 // Выбираем угол вниз:
6320 if FDirection = TDirection.D_LEFT then
6321 angle := ANGLE_LEFTDOWN
6322 else
6323 angle := ANGLE_RIGHTDOWN;
6325 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6326 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6328 // Если при угле вниз можно попасть в приблизительное положение цели:
6329 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6330 Target.X+Target.Rect.X+GetInterval(FDifficult.DiagPrecision, 128),
6331 Target.Y+Target.Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6332 Target.Rect.Width, Target.Rect.Height) and
6333 g_TraceVector(x1, y1, Target.cX, Target.cY) then
6334 begin // то нужно стрелять вниз
6335 SetAIFlag('NEEDFIRE', '1');
6336 SetAIFlag('NEEDSEEDOWN', '1');
6337 end;
6339 // Если цель видно и она на такой же высоте:
6340 if Target.Visible and
6341 (y1+4 < Target.Y+Target.Rect.Y+Target.Rect.Height) and
6342 (y1-4 > Target.Y+Target.Rect.Y) then
6343 begin
6344 // Если идем в сторону цели, то надо стрелять:
6345 if ((FDirection = TDirection.D_LEFT) and (Target.X < FObj.X)) or
6346 ((FDirection = TDirection.D_RIGHT) and (Target.X > FObj.X)) then
6347 begin // то нужно стрелять вперед
6348 SetAIFlag('NEEDFIRE', '1');
6349 SetAIFlag('NEEDSEEDOWN', '');
6350 SetAIFlag('NEEDSEEUP', '');
6351 end;
6352 // Если цель в пределах "экрана" и сложность позволяет прыжки сближения:
6353 if Abs(FObj.X-Target.X) < Trunc(gPlayerScreenSize.X*0.75) then
6354 if GetRnd(FDifficult.CloseJump) then
6355 begin // то если повезет - прыгаем (особенно, если близко)
6356 if Abs(FObj.X-Target.X) < 128 then
6357 a := 4
6358 else
6359 a := 30;
6360 if Random(a) = 0 then
6361 SetAIFlag('NEEDJUMP', '1');
6362 end;
6363 end;
6365 // Если цель все еще есть:
6366 if Target.UID <> 0 then
6367 if gTime-FLastVisible > 2000 then // Если видели давно
6368 Target.UID := 0 // то забыть цель
6369 else // Если видели недавно
6370 begin // но цель убили
6371 if Target.IsPlayer then
6372 begin // Цель - игрок
6373 pla := g_Player_Get(Target.UID);
6374 if (pla = nil) or (not pla.alive) or pla.NoTarget or
6375 (pla.FMegaRulez[MR_INVIS] >= gTime) then
6376 Target.UID := 0; // то забыть цель
6377 end
6378 else
6379 begin // Цель - монстр
6380 mon := g_Monsters_ByUID(Target.UID);
6381 if (mon = nil) or (not mon.alive) then
6382 Target.UID := 0; // то забыть цель
6383 end;
6384 end;
6385 end; // if Target.UID <> 0
6387 FTargetUID := Target.UID;
6389 // Если возможных целей нет:
6390 // (Атака чего-нибудь слева или справа)
6391 if targets = nil then
6392 if GetAIFlag('ATTACKLEFT') <> '' then
6393 begin // Если нужно атаковать налево
6394 RemoveAIFlag('ATTACKLEFT');
6396 SetAIFlag('NEEDJUMP', '1');
6398 if RunDirection() = TDirection.D_RIGHT then
6399 begin // Идем не в ту сторону
6400 if (Healthy() > 1) and GetRnd(FDifficult.InvisFire) then
6401 begin // Если здоровы, то, возможно, стреляем бежим влево и стреляем
6402 SetAIFlag('NEEDFIRE', '1');
6403 SetAIFlag('GOLEFT', '1');
6404 end;
6405 end
6406 else
6407 begin // Идем в нужную сторону
6408 if GetRnd(FDifficult.InvisFire) then // Возможно, стреляем вслепую
6409 SetAIFlag('NEEDFIRE', '1');
6410 if Healthy() <= 1 then // Побиты - убегаем
6411 SetAIFlag('GORIGHT', '1');
6412 end;
6413 end
6414 else
6415 if GetAIFlag('ATTACKRIGHT') <> '' then
6416 begin // Если нужно атаковать направо
6417 RemoveAIFlag('ATTACKRIGHT');
6419 SetAIFlag('NEEDJUMP', '1');
6421 if RunDirection() = TDirection.D_LEFT then
6422 begin // Идем не в ту сторону
6423 if (Healthy() > 1) and GetRnd(FDifficult.InvisFire) then
6424 begin // Если здоровы, то, возможно, бежим вправо и стреляем
6425 SetAIFlag('NEEDFIRE', '1');
6426 SetAIFlag('GORIGHT', '1');
6427 end;
6428 end
6429 else
6430 begin
6431 if GetRnd(FDifficult.InvisFire) then // Возможно, стреляем вслепую
6432 SetAIFlag('NEEDFIRE', '1');
6433 if Healthy() <= 1 then // Побиты - убегаем
6434 SetAIFlag('GOLEFT', '1');
6435 end;
6436 end;
6438 //HACK! (does it belongs there?)
6439 RealizeCurrentWeapon();
6441 // Если есть возможные цели:
6442 // (Стреляем по направлению к целям)
6443 if (targets <> nil) and (GetAIFlag('NEEDFIRE') <> '') then
6444 for a := 0 to High(targets) do
6445 begin
6446 // Если можем стрелять по диагонали:
6447 if GetRnd(FDifficult.DiagFire) then
6448 begin
6449 // Ищем цель сверху и стреляем, если есть:
6450 if FDirection = TDirection.D_LEFT then
6451 angle := ANGLE_LEFTUP
6452 else
6453 angle := ANGLE_RIGHTUP;
6455 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6456 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6458 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6459 targets[a].X+targets[a].Rect.X+GetInterval(FDifficult.DiagPrecision, 128),
6460 targets[a].Y+targets[a].Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6461 targets[a].Rect.Width, targets[a].Rect.Height) and
6462 g_TraceVector(x1, y1, targets[a].cX, targets[a].cY) then
6463 begin
6464 SetAIFlag('NEEDFIRE', '1');
6465 SetAIFlag('NEEDSEEUP', '1');
6466 end;
6468 // Ищем цель снизу и стреляем, если есть:
6469 if FDirection = TDirection.D_LEFT then
6470 angle := ANGLE_LEFTDOWN
6471 else
6472 angle := ANGLE_RIGHTDOWN;
6474 firew := Trunc(Cos(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6475 fireh := Trunc(Sin(DegToRad(-angle))*gPlayerScreenSize.X*0.6);
6477 if g_CollideLine(x1, y1, x1+firew, y1+fireh,
6478 targets[a].X+targets[a].Rect.X+GetInterval(FDifficult.DiagPrecision, 128),
6479 targets[a].Y+targets[a].Rect.Y+GetInterval(FDifficult.DiagPrecision, 128),
6480 targets[a].Rect.Width, targets[a].Rect.Height) and
6481 g_TraceVector(x1, y1, targets[a].cX, targets[a].cY) then
6482 begin
6483 SetAIFlag('NEEDFIRE', '1');
6484 SetAIFlag('NEEDSEEDOWN', '1');
6485 end;
6486 end;
6488 // Если цель "перед носом", то стреляем:
6489 if targets[a].Line and targets[a].Visible and
6490 (((FDirection = TDirection.D_LEFT) and (targets[a].X < FObj.X)) or
6491 ((FDirection = TDirection.D_RIGHT) and (targets[a].X > FObj.X))) then
6492 begin
6493 SetAIFlag('NEEDFIRE', '1');
6494 Break;
6495 end;
6496 end;
6498 // Если летит пуля, то, возможно, подпрыгиваем:
6499 if g_Weapon_Danger(FUID, FObj.X+PLAYER_RECT.X, FObj.Y+PLAYER_RECT.Y,
6500 PLAYER_RECT.Width, PLAYER_RECT.Height,
6501 40+GetInterval(FDifficult.Cover, 40)) then
6502 SetAIFlag('NEEDJUMP', '1');
6504 // Если кончились паторны, то нужно сменить оружие:
6505 ammo := GetAmmoByWeapon(FCurrWeap);
6506 if ((FCurrWeap = WEAPON_SHOTGUN2) and (ammo < 2)) or
6507 ((FCurrWeap = WEAPON_BFG) and (ammo < 40)) or
6508 (ammo = 0) then
6509 SetAIFlag('SELECTWEAPON', '1');
6511 // Если нужно сменить оружие, то выбираем нужное:
6512 if GetAIFlag('SELECTWEAPON') = '1' then
6513 begin
6514 SelectWeapon(-1);
6515 RemoveAIFlag('SELECTWEAPON');
6516 end;
6517 end;
6519 procedure TBot.Update();
6520 var
6521 EnableAI: Boolean;
6522 begin
6523 if not FAlive then
6524 begin // Respawn
6525 ReleaseKeys();
6526 PressKey(KEY_UP);
6527 end
6528 else
6529 begin
6530 EnableAI := True;
6532 // Проверяем, отключён ли AI ботов
6533 if (g_debug_BotAIOff = 1) and (Team = TEAM_RED) then
6534 EnableAI := False;
6535 if (g_debug_BotAIOff = 2) and (Team = TEAM_BLUE) then
6536 EnableAI := False;
6537 if g_debug_BotAIOff = 3 then
6538 EnableAI := False;
6540 if EnableAI then
6541 begin
6542 UpdateMove();
6543 UpdateCombat();
6544 end
6545 else
6546 begin
6547 RealizeCurrentWeapon();
6548 end;
6549 end;
6551 inherited Update();
6552 end;
6554 procedure TBot.ReleaseKey(Key: Byte);
6555 begin
6556 with FKeys[Key] do
6557 begin
6558 Pressed := False;
6559 Time := 0;
6560 end;
6561 end;
6563 function TBot.KeyPressed(Key: Word): Boolean;
6564 begin
6565 Result := FKeys[Key].Pressed;
6566 end;
6568 function TBot.GetAIFlag(aName: String20): String20;
6569 var
6570 a: Integer;
6571 begin
6572 Result := '';
6574 aName := LowerCase(aName);
6576 if FAIFlags <> nil then
6577 for a := 0 to High(FAIFlags) do
6578 if LowerCase(FAIFlags[a].Name) = aName then
6579 begin
6580 Result := FAIFlags[a].Value;
6581 Break;
6582 end;
6583 end;
6585 procedure TBot.RemoveAIFlag(aName: String20);
6586 var
6587 a, b: Integer;
6588 begin
6589 if FAIFlags = nil then Exit;
6591 aName := LowerCase(aName);
6593 for a := 0 to High(FAIFlags) do
6594 if LowerCase(FAIFlags[a].Name) = aName then
6595 begin
6596 if a <> High(FAIFlags) then
6597 for b := a to High(FAIFlags)-1 do
6598 FAIFlags[b] := FAIFlags[b+1];
6600 SetLength(FAIFlags, Length(FAIFlags)-1);
6601 Break;
6602 end;
6603 end;
6605 procedure TBot.SetAIFlag(aName, fValue: String20);
6606 var
6607 a: Integer;
6608 ok: Boolean;
6609 begin
6610 a := 0;
6611 ok := False;
6613 aName := LowerCase(aName);
6615 if FAIFlags <> nil then
6616 for a := 0 to High(FAIFlags) do
6617 if LowerCase(FAIFlags[a].Name) = aName then
6618 begin
6619 ok := True;
6620 Break;
6621 end;
6623 if ok then FAIFlags[a].Value := fValue
6624 else
6625 begin
6626 SetLength(FAIFlags, Length(FAIFlags)+1);
6627 with FAIFlags[High(FAIFlags)] do
6628 begin
6629 Name := aName;
6630 Value := fValue;
6631 end;
6632 end;
6633 end;
6635 procedure TBot.UpdateMove;
6637 procedure GoLeft(Time: Word = 1);
6638 begin
6639 ReleaseKey(KEY_LEFT);
6640 ReleaseKey(KEY_RIGHT);
6641 PressKey(KEY_LEFT, Time);
6642 SetDirection(TDirection.D_LEFT);
6643 end;
6645 procedure GoRight(Time: Word = 1);
6646 begin
6647 ReleaseKey(KEY_LEFT);
6648 ReleaseKey(KEY_RIGHT);
6649 PressKey(KEY_RIGHT, Time);
6650 SetDirection(TDirection.D_RIGHT);
6651 end;
6653 function Rnd(a: Word): Boolean;
6654 begin
6655 Result := Random(a) = 0;
6656 end;
6658 procedure Turn(Time: Word = 1200);
6659 begin
6660 if RunDirection() = TDirection.D_LEFT then GoRight(Time) else GoLeft(Time);
6661 end;
6663 procedure Stop();
6664 begin
6665 ReleaseKey(KEY_LEFT);
6666 ReleaseKey(KEY_RIGHT);
6667 end;
6669 function CanRunLeft(): Boolean;
6670 begin
6671 Result := not CollideLevel(-1, 0);
6672 end;
6674 function CanRunRight(): Boolean;
6675 begin
6676 Result := not CollideLevel(1, 0);
6677 end;
6679 function CanRun(): Boolean;
6680 begin
6681 if RunDirection() = TDirection.D_LEFT then Result := CanRunLeft() else Result := CanRunRight();
6682 end;
6684 procedure Jump(Time: Word = 30);
6685 begin
6686 PressKey(KEY_JUMP, Time);
6687 end;
6689 function NearHole(): Boolean;
6690 var
6691 x, sx: Integer;
6692 begin
6693 { TODO 5 : Лестницы }
6694 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6695 for x := 1 to PLAYER_RECT.Width do
6696 if (not StayOnStep(x*sx, 0)) and
6697 (not CollideLevel(x*sx, PLAYER_RECT.Height)) and
6698 (not CollideLevel(x*sx, PLAYER_RECT.Height*2)) then
6699 begin
6700 Result := True;
6701 Exit;
6702 end;
6704 Result := False;
6705 end;
6707 function BorderHole(): Boolean;
6708 var
6709 x, sx, xx: Integer;
6710 begin
6711 { TODO 5 : Лестницы }
6712 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6713 for x := 1 to PLAYER_RECT.Width do
6714 if (not StayOnStep(x*sx, 0)) and
6715 (not CollideLevel(x*sx, PLAYER_RECT.Height)) and
6716 (not CollideLevel(x*sx, PLAYER_RECT.Height*2)) then
6717 begin
6718 for xx := x to x+32 do
6719 if CollideLevel(xx*sx, PLAYER_RECT.Height) then
6720 begin
6721 Result := True;
6722 Exit;
6723 end;
6724 end;
6726 Result := False;
6727 end;
6729 function NearDeepHole(): Boolean;
6730 var
6731 x, sx, y: Integer;
6732 begin
6733 Result := False;
6735 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6736 y := 3;
6738 for x := 1 to PLAYER_RECT.Width do
6739 if (not StayOnStep(x*sx, 0)) and
6740 (not CollideLevel(x*sx, PLAYER_RECT.Height)) and
6741 (not CollideLevel(x*sx, PLAYER_RECT.Height*2)) then
6742 begin
6743 while FObj.Y+y*PLAYER_RECT.Height < gMapInfo.Height do
6744 begin
6745 if CollideLevel(x*sx, PLAYER_RECT.Height*y) then Exit;
6746 y := y+1;
6747 end;
6749 Result := True;
6750 end else Result := False;
6751 end;
6753 function OverDeepHole(): Boolean;
6754 var
6755 y: Integer;
6756 begin
6757 Result := False;
6759 y := 1;
6760 while FObj.Y+y*PLAYER_RECT.Height < gMapInfo.Height do
6761 begin
6762 if CollideLevel(0, PLAYER_RECT.Height*y) then Exit;
6763 y := y+1;
6764 end;
6766 Result := True;
6767 end;
6769 function OnGround(): Boolean;
6770 begin
6771 Result := StayOnStep(0, 0) or CollideLevel(0, 1);
6772 end;
6774 function OnLadder(): Boolean;
6775 begin
6776 Result := FullInStep(0, 0);
6777 end;
6779 function BelowLadder(): Boolean;
6780 begin
6781 Result := (FullInStep(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height) and
6782 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height)) or
6783 (FullInStep(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP) and
6784 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP));
6785 end;
6787 function BelowLiftUp(): Boolean;
6788 begin
6789 Result := ((FullInLift(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height) = -1) and
6790 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -PLAYER_RECT.Height)) or
6791 ((FullInLift(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP) = -1) and
6792 not CollideLevel(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*(PLAYER_RECT.Width div 2), -BOT_MAXJUMP));
6793 end;
6795 function OnTopLift(): Boolean;
6796 begin
6797 Result := (FullInLift(0, 0) = -1) and (FullInLift(0, -32) = 0);
6798 end;
6800 function CanJumpOver(): Boolean;
6801 var
6802 sx, y: Integer;
6803 begin
6804 sx := IfThen(RunDirection() = TDirection.D_LEFT, -1, 1);
6806 Result := False;
6808 if not CollideLevel(sx, 0) then Exit;
6810 for y := 1 to BOT_MAXJUMP do
6811 if CollideLevel(0, -y) then Exit else
6812 if not CollideLevel(sx, -y) then
6813 begin
6814 Result := True;
6815 Exit;
6816 end;
6817 end;
6819 function CanJumpUp(Dist: ShortInt): Boolean;
6820 var
6821 y, yy: Integer;
6822 c: Boolean;
6823 begin
6824 Result := False;
6826 if CollideLevel(Dist, 0) then Exit;
6828 c := False;
6829 for y := 0 to BOT_MAXJUMP do
6830 if CollideLevel(Dist, -y) then
6831 begin
6832 c := True;
6833 Break;
6834 end;
6836 if not c then Exit;
6838 c := False;
6839 for yy := y+1 to BOT_MAXJUMP do
6840 if not CollideLevel(Dist, -yy) then
6841 begin
6842 c := True;
6843 Break;
6844 end;
6846 if not c then Exit;
6848 c := False;
6849 for y := 0 to BOT_MAXJUMP do
6850 if CollideLevel(0, -y) then
6851 begin
6852 c := True;
6853 Break;
6854 end;
6856 if c then Exit;
6858 if y < yy then Exit;
6860 Result := True;
6861 end;
6863 function IsSafeTrigger(): Boolean;
6864 var
6865 a: Integer;
6866 begin
6867 Result := True;
6868 if gTriggers = nil then
6869 Exit;
6870 for a := 0 to High(gTriggers) do
6871 if Collide(gTriggers[a].X,
6872 gTriggers[a].Y,
6873 gTriggers[a].Width,
6874 gTriggers[a].Height) and
6875 (gTriggers[a].TriggerType in [TRIGGER_EXIT, TRIGGER_CLOSEDOOR,
6876 TRIGGER_CLOSETRAP, TRIGGER_TRAP,
6877 TRIGGER_PRESS, TRIGGER_ON, TRIGGER_OFF,
6878 TRIGGER_ONOFF, TRIGGER_SPAWNMONSTER,
6879 TRIGGER_DAMAGE, TRIGGER_SHOT]) then
6880 Result := False;
6881 end;
6883 begin
6884 // Возможно, нажимаем кнопку:
6885 if Rnd(16) and IsSafeTrigger() then
6886 PressKey(KEY_OPEN);
6888 // Если под лифтом или ступеньками, то, возможно, прыгаем:
6889 if OnLadder() or ((BelowLadder() or BelowLiftUp()) and Rnd(8)) then
6890 begin
6891 ReleaseKey(KEY_LEFT);
6892 ReleaseKey(KEY_RIGHT);
6893 Jump();
6894 end;
6896 // Идем влево, если надо было:
6897 if GetAIFlag('GOLEFT') <> '' then
6898 begin
6899 RemoveAIFlag('GOLEFT');
6900 if CanRunLeft() then
6901 GoLeft(360);
6902 end;
6904 // Идем вправо, если надо было:
6905 if GetAIFlag('GORIGHT') <> '' then
6906 begin
6907 RemoveAIFlag('GORIGHT');
6908 if CanRunRight() then
6909 GoRight(360);
6910 end;
6912 // Если вылетели за карту, то пробуем вернуться:
6913 if FObj.X < -32 then
6914 GoRight(360)
6915 else
6916 if FObj.X+32 > gMapInfo.Width then
6917 GoLeft(360);
6919 // Прыгаем, если надо было:
6920 if GetAIFlag('NEEDJUMP') <> '' then
6921 begin
6922 Jump(0);
6923 RemoveAIFlag('NEEDJUMP');
6924 end;
6926 // Смотрим вверх, если надо было:
6927 if GetAIFlag('NEEDSEEUP') <> '' then
6928 begin
6929 ReleaseKey(KEY_UP);
6930 ReleaseKey(KEY_DOWN);
6931 PressKey(KEY_UP, 20);
6932 RemoveAIFlag('NEEDSEEUP');
6933 end;
6935 // Смотрим вниз, если надо было:
6936 if GetAIFlag('NEEDSEEDOWN') <> '' then
6937 begin
6938 ReleaseKey(KEY_UP);
6939 ReleaseKey(KEY_DOWN);
6940 PressKey(KEY_DOWN, 20);
6941 RemoveAIFlag('NEEDSEEDOWN');
6942 end;
6944 // Если нужно было в дыру и мы не на земле, то покорно летим:
6945 if GetAIFlag('GOINHOLE') <> '' then
6946 if not OnGround() then
6947 begin
6948 ReleaseKey(KEY_LEFT);
6949 ReleaseKey(KEY_RIGHT);
6950 RemoveAIFlag('GOINHOLE');
6951 SetAIFlag('FALLINHOLE', '1');
6952 end;
6954 // Если падали и достигли земли, то хватит падать:
6955 if GetAIFlag('FALLINHOLE') <> '' then
6956 if OnGround() then
6957 RemoveAIFlag('FALLINHOLE');
6959 // Если летели прямо и сейчас не на лестнице или на вершине лифта, то отходим в сторону:
6960 if not (KeyPressed(KEY_LEFT) or KeyPressed(KEY_RIGHT)) then
6961 if GetAIFlag('FALLINHOLE') = '' then
6962 if (not OnLadder()) or (FObj.Vel.Y >= 0) or (OnTopLift()) then
6963 if Rnd(2) then
6964 GoLeft(360)
6965 else
6966 GoRight(360);
6968 // Если на земле и можно подпрыгнуть, то, возможно, прыгаем:
6969 if OnGround() and
6970 CanJumpUp(IfThen(RunDirection() = TDirection.D_LEFT, -1, 1)*32) and
6971 Rnd(8) then
6972 Jump();
6974 // Если на земле и возле дыры (глубина > 2 ростов игрока):
6975 if OnGround() and NearHole() then
6976 if NearDeepHole() then // Если это бездна
6977 case Random(6) of
6978 0..3: Turn(); // Бежим обратно
6979 4: Jump(); // Прыгаем
6980 5: begin // Прыгаем обратно
6981 Turn();
6982 Jump();
6983 end;
6984 end
6985 else // Это не бездна и мы еще не летим туда
6986 if GetAIFlag('GOINHOLE') = '' then
6987 case Random(6) of
6988 0: Turn(); // Не нужно туда
6989 1: Jump(); // Вдруг повезет - прыгаем
6990 else // Если яма с границей, то при случае можно туда прыгнуть
6991 if BorderHole() then
6992 SetAIFlag('GOINHOLE', '1');
6993 end;
6995 // Если на земле, но некуда идти:
6996 if (not CanRun()) and OnGround() then
6997 begin
6998 // Если мы на лестнице или можно перепрыгнуть, то прыгаем:
6999 if CanJumpOver() or OnLadder() then
7000 Jump()
7001 else // иначе попытаемся в другую сторону
7002 if Random(2) = 0 then
7003 begin
7004 if IsSafeTrigger() then
7005 PressKey(KEY_OPEN);
7006 end else
7007 Turn();
7008 end;
7010 // Осталось мало воздуха:
7011 if FAir < 36 * 2 then
7012 Jump(20);
7014 // Выбираемся из кислоты, если нет костюма, обожглись, или мало здоровья:
7015 if (FMegaRulez[MR_SUIT] < gTime) and ((FLastHit = HIT_ACID) or (Healthy() <= 1)) then
7016 if BodyInAcid(0, 0) then
7017 Jump();
7018 end;
7020 function TBot.FullInStep(XInc, YInc: Integer): Boolean;
7021 begin
7022 Result := g_Map_CollidePanel(FObj.X+PLAYER_RECT.X+XInc, FObj.Y+PLAYER_RECT.Y+YInc,
7023 PLAYER_RECT.Width, PLAYER_RECT.Height, PANEL_STEP, False);
7024 end;
7026 {function TBot.NeedItem(Item: Byte): Byte;
7027 begin
7028 Result := 4;
7029 end;}
7031 procedure TBot.SelectWeapon(Dist: Integer);
7032 var
7033 a: Integer;
7035 function HaveAmmo(weapon: Byte): Boolean;
7036 begin
7037 case weapon of
7038 WEAPON_PISTOL: Result := FAmmo[A_BULLETS] >= 1;
7039 WEAPON_SHOTGUN1: Result := FAmmo[A_SHELLS] >= 1;
7040 WEAPON_SHOTGUN2: Result := FAmmo[A_SHELLS] >= 2;
7041 WEAPON_CHAINGUN: Result := FAmmo[A_BULLETS] >= 10;
7042 WEAPON_ROCKETLAUNCHER: Result := FAmmo[A_ROCKETS] >= 1;
7043 WEAPON_PLASMA: Result := FAmmo[A_CELLS] >= 10;
7044 WEAPON_BFG: Result := FAmmo[A_CELLS] >= 40;
7045 WEAPON_SUPERPULEMET: Result := FAmmo[A_SHELLS] >= 1;
7046 WEAPON_FLAMETHROWER: Result := FAmmo[A_FUEL] >= 1;
7047 else Result := True;
7048 end;
7049 end;
7051 begin
7052 if Dist = -1 then Dist := BOT_LONGDIST;
7054 if Dist > BOT_LONGDIST then
7055 begin // Дальний бой
7056 for a := 0 to 9 do
7057 if FWeapon[FDifficult.WeaponPrior[a]] and HaveAmmo(FDifficult.WeaponPrior[a]) then
7058 begin
7059 FSelectedWeapon := FDifficult.WeaponPrior[a];
7060 Break;
7061 end;
7062 end
7063 else //if Dist > BOT_UNSAFEDIST then
7064 begin // Ближний бой
7065 for a := 0 to 9 do
7066 if FWeapon[FDifficult.CloseWeaponPrior[a]] and HaveAmmo(FDifficult.CloseWeaponPrior[a]) then
7067 begin
7068 FSelectedWeapon := FDifficult.CloseWeaponPrior[a];
7069 Break;
7070 end;
7071 end;
7072 { else
7073 begin
7074 for a := 0 to 9 do
7075 if FWeapon[FDifficult.SafeWeaponPrior[a]] and HaveAmmo(FDifficult.SafeWeaponPrior[a]) then
7076 begin
7077 FSelectedWeapon := FDifficult.SafeWeaponPrior[a];
7078 Break;
7079 end;
7080 end;}
7081 end;
7083 function TBot.PickItem(ItemType: Byte; force: Boolean; var remove: Boolean): Boolean;
7084 begin
7085 Result := inherited PickItem(ItemType, force, remove);
7087 if Result then SetAIFlag('SELECTWEAPON', '1');
7088 end;
7090 function TBot.Heal(value: Word; Soft: Boolean): Boolean;
7091 begin
7092 Result := inherited Heal(value, Soft);
7093 end;
7095 function TBot.Healthy(): Byte;
7096 begin
7097 if FMegaRulez[MR_INVUL] >= gTime then Result := 3
7098 else if (FHealth > 80) or ((FHealth > 50) and (FArmor > 20)) then Result := 3
7099 else if (FHealth > 50) then Result := 2
7100 else if (FHealth > 20) then Result := 1
7101 else Result := 0;
7102 end;
7104 function TBot.TargetOnScreen(TX, TY: Integer): Boolean;
7105 begin
7106 Result := (Abs(FObj.X-TX) <= Trunc(gPlayerScreenSize.X*0.6)) and
7107 (Abs(FObj.Y-TY) <= Trunc(gPlayerScreenSize.Y*0.6));
7108 end;
7110 procedure TBot.OnDamage(Angle: SmallInt);
7111 var
7112 pla: TPlayer;
7113 mon: TMonster;
7114 ok: Boolean;
7115 begin
7116 inherited;
7118 if (Angle = 0) or (Angle = 180) then
7119 begin
7120 ok := False;
7121 if (g_GetUIDType(FLastSpawnerUID) = UID_PLAYER) and
7122 LongBool(gGameSettings.Options and GAME_OPTION_BOTVSPLAYER) then
7123 begin // Игрок
7124 pla := g_Player_Get(FLastSpawnerUID);
7125 ok := not TargetOnScreen(pla.FObj.X + PLAYER_RECT.X,
7126 pla.FObj.Y + PLAYER_RECT.Y);
7127 end
7128 else
7129 if (g_GetUIDType(FLastSpawnerUID) = UID_MONSTER) and
7130 LongBool(gGameSettings.Options and GAME_OPTION_BOTVSMONSTER) then
7131 begin // Монстр
7132 mon := g_Monsters_ByUID(FLastSpawnerUID);
7133 ok := not TargetOnScreen(mon.Obj.X + mon.Obj.Rect.X,
7134 mon.Obj.Y + mon.Obj.Rect.Y);
7135 end;
7137 if ok then
7138 if Angle = 0 then
7139 SetAIFlag('ATTACKLEFT', '1')
7140 else
7141 SetAIFlag('ATTACKRIGHT', '1');
7142 end;
7143 end;
7145 function TBot.RunDirection(): TDirection;
7146 begin
7147 if Abs(Vel.X) >= 1 then
7148 begin
7149 if Vel.X > 0 then Result := TDirection.D_RIGHT else Result := TDirection.D_LEFT;
7150 end else
7151 Result := FDirection;
7152 end;
7154 function TBot.GetRnd(a: Byte): Boolean;
7155 begin
7156 if a = 0 then Result := False
7157 else if a = 255 then Result := True
7158 else Result := Random(256) > 255-a;
7159 end;
7161 function TBot.GetInterval(a: Byte; radius: SmallInt): SmallInt;
7162 begin
7163 Result := Round((255-a)/255*radius*(Random(2)-1));
7164 end;
7167 procedure TDifficult.save (st: TStream);
7168 begin
7169 utils.writeInt(st, Byte(DiagFire));
7170 utils.writeInt(st, Byte(InvisFire));
7171 utils.writeInt(st, Byte(DiagPrecision));
7172 utils.writeInt(st, Byte(FlyPrecision));
7173 utils.writeInt(st, Byte(Cover));
7174 utils.writeInt(st, Byte(CloseJump));
7175 st.WriteBuffer(WeaponPrior[Low(WeaponPrior)], sizeof(WeaponPrior));
7176 st.WriteBuffer(CloseWeaponPrior[Low(CloseWeaponPrior)], sizeof(CloseWeaponPrior));
7177 end;
7179 procedure TDifficult.load (st: TStream);
7180 begin
7181 DiagFire := utils.readByte(st);
7182 InvisFire := utils.readByte(st);
7183 DiagPrecision := utils.readByte(st);
7184 FlyPrecision := utils.readByte(st);
7185 Cover := utils.readByte(st);
7186 CloseJump := utils.readByte(st);
7187 st.ReadBuffer(WeaponPrior[Low(WeaponPrior)], sizeof(WeaponPrior));
7188 st.ReadBuffer(CloseWeaponPrior[Low(CloseWeaponPrior)], sizeof(CloseWeaponPrior));
7189 end;
7192 procedure TBot.SaveState (st: TStream);
7193 var
7194 i: Integer;
7195 dw: Integer;
7196 begin
7197 inherited SaveState(st);
7198 utils.writeSign(st, 'BOT0');
7199 // Выбранное оружие
7200 utils.writeInt(st, Byte(FSelectedWeapon));
7201 // UID цели
7202 utils.writeInt(st, Word(FTargetUID));
7203 // Время потери цели
7204 utils.writeInt(st, LongWord(FLastVisible));
7205 // Количество флагов ИИ
7206 dw := Length(FAIFlags);
7207 utils.writeInt(st, LongInt(dw));
7208 // Флаги ИИ
7209 for i := 0 to dw-1 do
7210 begin
7211 utils.writeStr(st, FAIFlags[i].Name, 20);
7212 utils.writeStr(st, FAIFlags[i].Value, 20);
7213 end;
7214 // Настройки сложности
7215 FDifficult.save(st);
7216 end;
7219 procedure TBot.LoadState (st: TStream);
7220 var
7221 i: Integer;
7222 dw: Integer;
7223 begin
7224 inherited LoadState(st);
7225 if not utils.checkSign(st, 'BOT0') then raise XStreamError.Create('invalid bot signature');
7226 // Выбранное оружие
7227 FSelectedWeapon := utils.readByte(st);
7228 // UID цели
7229 FTargetUID := utils.readWord(st);
7230 // Время потери цели
7231 FLastVisible := utils.readLongWord(st);
7232 // Количество флагов ИИ
7233 dw := utils.readLongInt(st);
7234 if (dw < 0) or (dw > 16384) then raise XStreamError.Create('invalid number of bot AI flags');
7235 SetLength(FAIFlags, dw);
7236 // Флаги ИИ
7237 for i := 0 to dw-1 do
7238 begin
7239 FAIFlags[i].Name := utils.readStr(st, 20);
7240 FAIFlags[i].Value := utils.readStr(st, 20);
7241 end;
7242 // Настройки сложности
7243 FDifficult.load(st);
7244 end;
7247 begin
7248 conRegVar('cheat_berserk_autoswitch', @gBerserkAutoswitch, 'autoswitch to fist when berserk pack taken', '', true, true);
7249 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');
7250 conRegVar('player_indicator_style', @gPlayerIndicatorStyle, 'Visual appearance of indicator', 'Visual appearance of indicator');
7251 end.