X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fgame%2Fg_monsters.pas;h=b7d217b159a306c27aadb399139786a38fead474;hb=d7d166dc3cd287276202e862746208892c4cc89f;hp=5fb769dbdab703eb9daff973c5877a6ea556025b;hpb=3cde28288f16b317ae049cc692aa95536f297d2d;p=d2df-sdl.git diff --git a/src/game/g_monsters.pas b/src/game/g_monsters.pas index 5fb769d..b7d217b 100644 --- a/src/game/g_monsters.pas +++ b/src/game/g_monsters.pas @@ -14,13 +14,17 @@ * along with this program. If not, see . *) {$INCLUDE ../shared/a_modes.inc} +{$M+} +{.$DEFINE D2F_DEBUG_MONS_MOVE} unit g_monsters; interface uses + SysUtils, Classes, + mempool, g_basic, e_graphics, g_phys, g_textures, g_grid, - g_saveload, BinEditor, g_panel, xprofiler; + g_saveload, g_panel, xprofiler; const MONSTATE_SLEEP = 0; @@ -36,15 +40,17 @@ const MONSTATE_REVIVE = 10; MONSTATE_RUNOUT = 11; +{ in mapdef now BH_NORMAL = 0; BH_KILLER = 1; BH_MANIAC = 2; BH_INSANE = 3; BH_CANNIBAL = 4; BH_GOOD = 5; +} type - TMonster = Class (TObject) + TMonster = class(TPoolObject) private FMonsterType: Byte; FUID: Word; @@ -56,7 +62,7 @@ type FMaxHealth: Integer; FState: Byte; FCurAnim: Byte; - FAnim: Array of Array [D_LEFT..D_RIGHT] of TAnimation; + FAnim: Array of Array [TDirection.D_LEFT..TDirection.D_RIGHT] of TAnimation; FTargetUID: Word; FTargetTime: Integer; FBehaviour: Byte; @@ -85,14 +91,24 @@ type FDieTriggers: Array of Integer; FSpawnTrigger: Integer; + mNeedSend: Boolean; // for network + + mEDamageType: Integer; + procedure Turn(); function findNewPrey(): Boolean; procedure ActivateTriggers(); + procedure setGameX (v: Integer); inline; + procedure setGameY (v: Integer); inline; + + procedure doDamage (v: Integer); + public FNoRespawn: Boolean; FFireTime: Integer; trapCheckFrameId: DWord; // for `g_weapons.CheckTrap()` + mplatCheckFrameId: LongWord; constructor Create(MonsterType: Byte; aID: Integer; ForcedUID: Integer = -1); destructor Destroy(); override; @@ -100,7 +116,7 @@ type function Collide(Panel: TPanel): Boolean; overload; function Collide(X, Y: Integer): Boolean; overload; function TeleportTo(X, Y: Integer; silent: Boolean; dir: Byte): Boolean; - function Live(): Boolean; + function alive(): Boolean; procedure SetHealth(aH: Integer); procedure Push(vx, vy: Integer); function Damage(aDamage: Word; VelX, VelY: Integer; SpawnerUID: Word; t: Byte): Boolean; @@ -119,8 +135,8 @@ type procedure AddTrigger(t: Integer); procedure ClearTriggers(); procedure Respawn(); - procedure SaveState(var Mem: TBinMemoryWriter); - procedure LoadState(var Mem: TBinMemoryReader); + procedure SaveState (st: TStream); + procedure LoadState (st: TStream); procedure SetState(State: Byte; ForceAnim: Byte = 255); procedure MakeBloodVector(Count: Word; VelX, VelY: Integer); procedure MakeBloodSimple(Count: Word); @@ -133,8 +149,21 @@ type procedure positionChanged (); //WARNING! call this after monster position was changed, or coldet will not work right! + procedure setPosition (ax, ay: Integer; callPosChanged: Boolean=true); inline; + procedure moveBy (dx, dy: Integer); inline; + procedure getMapBox (out x, y, w, h: Integer); inline; + // get-and-clear + function gncNeedSend (): Boolean; inline; + procedure setDirty (); inline; // why `dirty`? 'cause i may introduce property `needSend` later + + public + property Obj: TObj read FObj; + + property proxyId: Integer read mProxyId; + property arrIdx: Integer read mArrIdx; + property MonsterType: Byte read FMonsterType; property MonsterHealth: Integer read FHealth write FHealth; property MonsterAmmo: Integer read FAmmo write FAmmo; @@ -147,12 +176,11 @@ type property MonsterPain: Integer read FPain write FPain; property MonsterAnim: Byte read FCurAnim write FCurAnim; - property Obj: TObj read FObj; property UID: Word read FUID write FUID; property SpawnTrigger: Integer read FSpawnTrigger write FSpawnTrigger; - property GameX: Integer read FObj.X write FObj.X; - property GameY: Integer read FObj.Y write FObj.Y; + property GameX: Integer read FObj.X write setGameX; + property GameY: Integer read FObj.Y write setGameY; property GameVelX: Integer read FObj.Vel.X write FObj.Vel.X; property GameVelY: Integer read FObj.Vel.Y write FObj.Vel.Y; property GameAccelX: Integer read FObj.Accel.X write FObj.Accel.X; @@ -161,8 +189,35 @@ type property StartID: Integer read FStartID; - property proxyId: Integer read mProxyId; - property arrIdx: Integer read mArrIdx; + published + property eMonsterType: Byte read FMonsterType; + property eMonsterHealth: Integer read FHealth write FHealth; + property eMonsterAmmo: Integer read FAmmo write FAmmo; + property eMonsterTargetUID: Word read FTargetUID write FTargetUID; + property eMonsterTargetTime: Integer read FTargetTime write FTargetTime; + property eMonsterBehaviour: Byte read FBehaviour write FBehaviour; + property eMonsterSleep: Integer read FSleep write FSleep; + property eMonsterState: Byte read FState write FState; + property eMonsterRemoved: Boolean read FRemoved; + property eMonsterPain: Integer read FPain write FPain; + property eMonsterAnim: Byte read FCurAnim; + + property eUID: Word read FUID; + property eSpawnTrigger: Integer read FSpawnTrigger; + + property eGameX: Integer read FObj.X write setGameX; + property eGameY: Integer read FObj.Y write setGameY; + property eGameVelX: Integer read FObj.Vel.X write FObj.Vel.X; + property eGameVelY: Integer read FObj.Vel.Y write FObj.Vel.Y; + property eGameAccelX: Integer read FObj.Accel.X write FObj.Accel.X; + property eGameAccelY: Integer read FObj.Accel.Y write FObj.Accel.Y; + property eGameDirection: TDirection read FDirection write FDirection; + + property eStartID: Integer read FStartID; + + // set this before assigning something to `eDamage` + property eDamageType: Integer read mEDamageType write mEDamageType; + property eDamage: Integer write doDamage; end; @@ -172,7 +227,7 @@ procedure g_Mons_InitTree (x, y, w, h: Integer); procedure g_Monsters_LoadData (); procedure g_Monsters_FreeData (); procedure g_Monsters_Init (); -procedure g_Monsters_Free (); +procedure g_Monsters_Free (clearGrid: Boolean=true); function g_Monsters_Create (MonsterType: Byte; X, Y: Integer; Direction: TDirection; AdjCoord: Boolean = False; ForcedUID: Integer = -1): TMonster; procedure g_Monsters_Update (); @@ -180,11 +235,18 @@ procedure g_Monsters_Draw (); procedure g_Monsters_DrawHealth (); function g_Monsters_ByUID (UID: Word): TMonster; procedure g_Monsters_killedp (); -procedure g_Monsters_SaveState (var Mem: TBinMemoryWriter); -procedure g_Monsters_LoadState (var Mem: TBinMemoryReader); -function g_Monsters_GetIDByName (name: String): Integer; -function g_Monsters_GetNameByID (MonsterType: Byte): String; -function g_Monsters_GetKilledBy (MonsterType: Byte): String; +procedure g_Monsters_SaveState (st: TStream); +procedure g_Monsters_LoadState (st: TStream); + +function g_Mons_SpawnAt (monType: Integer; x, y: Integer; dir: TDirection=TDirection.D_LEFT): TMonster; overload; +function g_Mons_SpawnAt (const typeName: AnsiString; x, y: Integer; dir: TDirection=TDirection.D_LEFT): TMonster; overload; + +function g_Mons_TypeLo (): Integer; inline; +function g_Mons_TypeHi (): Integer; inline; + +function g_Mons_TypeIdByName (const name: AnsiString): Integer; +function g_Mons_NameByTypeId (monType: Integer): AnsiString; +function g_Mons_GetKilledByTypeId (monType: Integer): AnsiString; type @@ -206,7 +268,8 @@ function g_Mons_ForEachAlive (cb: TEachMonsterCB): Boolean; function g_Mons_ForEachAt (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean; function g_Mons_ForEachAliveAt (x, y: Integer; width, height: Integer; cb: TEachMonsterCB): Boolean; -function g_Mons_getNewTrapFrameId (): DWord; +function g_Mons_getNewTrapFrameId (): DWord; inline; +function g_Mons_getNewMPlatFrameId (): LongWord; inline; type @@ -234,7 +297,7 @@ type TMonsterGrid = specialize TBodyGridBase; var - monsGrid: TMonsterGrid = nil; + monsGrid: TMonsterGrid = nil; // DO NOT USE! public for debugging only! var @@ -245,10 +308,11 @@ var implementation uses - e_log, g_main, g_sound, g_gfx, g_player, g_game, + e_log, e_texture, g_main, g_sound, g_gfx, g_player, g_game, g_weapons, g_triggers, MAPDEF, g_items, g_options, - g_console, g_map, Math, SysUtils, g_menu, wadreader, - g_language, g_netmsg; + g_console, g_map, Math, g_menu, wadreader, + g_language, g_netmsg, idpool, utils, xstreams; + // ////////////////////////////////////////////////////////////////////////// // @@ -281,7 +345,8 @@ end; // ////////////////////////////////////////////////////////////////////////// // var - monCheckTrapLastFrameId: DWord; + monCheckTrapLastFrameId: DWord = 0; + monCheckMPlatLastFrameId: LongWord = 0; procedure TMonster.getMapBox (out x, y, w, h: Integer); inline; @@ -292,6 +357,10 @@ begin h := FObj.Rect.Height; end; +function TMonster.gncNeedSend (): Boolean; inline; begin result := mNeedSend; mNeedSend := false; end; + +procedure TMonster.setDirty (); inline; begin mNeedSend := true; end; + // ////////////////////////////////////////////////////////////////////////// // function g_Mons_AlongLine (x0, y0, x1, y1: Integer; cb: TMonsAlongLineCB; log: Boolean=false): TMonster; @@ -304,35 +373,49 @@ end; //WARNING! call this after monster position was changed, or coldet will not work right! procedure TMonster.positionChanged (); var - x, y: Integer; + x, y, w, h: Integer; + nx, ny, nw, nh: Integer; begin {$IF DEFINED(D2F_DEBUG_MONS_MOVE)} //e_WriteLog(Format('monster #%d(%u): pos=(%d,%d); rpos=(%d,%d)', [mArrIdx, UID, FObj.X, FObj.Y, FObj.Rect.X, FObj.Rect.Y]), MSG_NOTIFY); {$ENDIF} if (mProxyId = -1) then begin + //mNeedSend := true; mProxyId := monsGrid.insertBody(self, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height); {$IF DEFINED(D2F_DEBUG_MONS_MOVE)} monsGrid.getBodyXY(mProxyId, x, y); - e_WriteLog(Format('monster #%d(%u): inserted into the grid; mProxyid=%d; x=%d; y=%d', [mArrIdx, UID, mProxyId, x, y]), MSG_NOTIFY); + e_WriteLog(Format('monster #%d:(%u): inserted into the grid; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY); {$ENDIF} end else begin - monsGrid.getBodyXY(mProxyId, x, y); - if (FObj.X+FObj.Rect.X = x) and (FObj.Y+FObj.Rect.Y = y) then exit; // nothing to do - {$IF DEFINED(D2F_DEBUG_MONS_MOVE)}e_WriteLog(Format('monster #%d(%u): updating tree; mProxyid=%d; x=%d; y=%d', [mArrIdx, UID, mProxyId, x, y]), MSG_NOTIFY);{$ENDIF} - - {$IF TRUE} - monsGrid.moveBody(mProxyId, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y); - {$ELSE} - monsGrid.removeBody(mProxyId); - mProxyId := monsGrid.insertBody(self, FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height); - {$ENDIF} + monsGrid.getBodyDims(mProxyId, x, y, w, h); + getMapBox(nx, ny, nw, nh); + if (w <> nw) or (h <> nh) then + begin + //mNeedSend := true; + {$IF DEFINED(D2F_DEBUG_MONS_MOVE)} + e_WriteLog(Format('monster #%d:(%u): resized; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY); + {$ENDIF} + monsGrid.moveResizeBody(mProxyId, nx, ny, nw, nh); + end + else if (x <> nx) or (y <> ny) then + begin + //mNeedSend := true; + {$IF DEFINED(D2F_DEBUG_MONS_MOVE)} + e_WriteLog(Format('monster #%d:(%u): updating grid; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY); + {$ENDIF} + monsGrid.moveBody(mProxyId, nx, ny); + end + else + begin + exit; // nothing to do + end; {$IF DEFINED(D2F_DEBUG_MONS_MOVE)} monsGrid.getBodyXY(mProxyId, x, y); - e_WriteLog(Format('monster #%d(%u): updated tree; mProxyid=%d; x=%d; y=%d', [mArrIdx, UID, mProxyId, x, y]), MSG_NOTIFY); + e_WriteLog(Format('monster #%d:(%u): updated grid; mProxyid=%d; gx=%d; gy=%d', [mArrIdx, UID, mProxyId, x-monsGrid.gridX0, y-monsGrid.gridY0]), MSG_NOTIFY); {$ENDIF} end; end; @@ -440,8 +523,8 @@ const LeftAnim: Boolean; wX, wY: Integer; // Îòêóäà âûëåòèò ïóëÿ AnimSpeed: Array [ANIM_SLEEP..ANIM_PAIN] of Byte; - AnimDeltaRight: Array [ANIM_SLEEP..ANIM_PAIN] of TPoint; - AnimDeltaLeft: Array [ANIM_SLEEP..ANIM_PAIN] of TPoint; + AnimDeltaRight: Array [ANIM_SLEEP..ANIM_PAIN] of TDFPoint; + AnimDeltaLeft: Array [ANIM_SLEEP..ANIM_PAIN] of TDFPoint; end = // SLEEP GO DIE MESS ATTACK ATTACK2 PAIN ((LeftAnim: False; wX: 54; wY: 32; AnimSpeed:(3, 2, 3, 2, 3, 0, 4); //DEMON AnimDeltaRight: ((X: 1; Y: 4), (X: 1; Y: 4), (X: 0; Y: 4), (X: 0; Y: 4), (X: 2; Y: 6), (X: 2; Y: 6), (X: 2; Y: 5)); @@ -527,9 +610,11 @@ const MAX_SOUL = 512; // Îãðàíè÷åíèå Lost_Soul'îâ +// ////////////////////////////////////////////////////////////////////////// // var gMonsters: array of TMonster; uidMap: array [0..65535] of TMonster; // monster knows it's index + freeInds: TIdPool = nil; procedure clearUidMap (); @@ -537,15 +622,16 @@ var idx: Integer; begin for idx := 0 to High(uidMap) do uidMap[idx] := nil; + freeInds.clear(); end; -function g_Mons_getNewTrapFrameId (): DWord; +function g_Mons_getNewTrapFrameId (): DWord; inline; var f: Integer; begin Inc(monCheckTrapLastFrameId); - if monCheckTrapLastFrameId = 0 then + if (monCheckTrapLastFrameId = 0) then begin // wraparound monCheckTrapLastFrameId := 1; @@ -558,6 +644,24 @@ begin end; +function g_Mons_getNewMPlatFrameId (): LongWord; inline; +var + f: Integer; +begin + Inc(monCheckMPlatLastFrameId); + if (monCheckMPlatLastFrameId = 0) then + begin + // wraparound + monCheckMPlatLastFrameId := 1; + for f := 0 to High(gMonsters) do + begin + if (gMonsters[f] <> nil) then gMonsters[f].mplatCheckFrameId := 0; + end; + end; + result := monCheckMPlatLastFrameId; +end; + + var pt_x: Integer = 0; pt_xs: Integer = 1; @@ -566,29 +670,16 @@ var soulcount: Integer = 0; -function allocMonster(): DWORD; +function allocMonster (): DWORD; var - i, olen: Integer; + f, olen: Integer; begin - for i := 0 to High(gMonsters) do - begin - if (gMonsters[i] = nil) then - begin - result := i; - exit; - end; - end; - - olen := Length(gMonsters); - if (olen = 0) then - begin - SetLength(gMonsters, 64); - result := 0; - end - else + result := freeInds.alloc(); + if (result > High(gMonsters)) then begin - result := olen; - SetLength(gMonsters, Length(gMonsters)+32); + olen := Length(gMonsters); + SetLength(gMonsters, result+64); + for f := olen to High(gMonsters) do gMonsters[f] := nil; end; end; @@ -718,7 +809,7 @@ end; procedure g_Monsters_LoadData(); begin - e_WriteLog('Loading monsters data...', MSG_NOTIFY); + e_WriteLog('Loading monsters data...', TMsgType.Notify); g_Game_SetLoadingText(_lc[I_LOAD_MONSTER_TEXTURES]+' 0%', 0, False); g_Frames_CreateWAD(nil, 'FRAMES_MONSTER_BARREL_SLEEP', GameWAD+':MTEXTURES\BARREL_SLEEP', 64, 64, 3); @@ -954,13 +1045,15 @@ begin g_Sound_CreateWADEx('SOUND_MONSTER_FISH_ATTACK', GameWAD+':MSOUNDS\FISH_ATTACK'); + freeInds := TIdPool.Create(); clearUidMap(); monCheckTrapLastFrameId := 0; + monCheckMPlatLastFrameId := 0; end; procedure g_Monsters_FreeData(); begin - e_WriteLog('Releasing monsters data...', MSG_NOTIFY); + e_WriteLog('Releasing monsters data...', TMsgType.Notify); g_Frames_DeleteByName('FRAMES_MONSTER_BARREL_SLEEP'); g_Frames_DeleteByName('FRAMES_MONSTER_BARREL_PAIN'); @@ -1172,6 +1265,9 @@ begin g_Sound_Delete('SOUND_MONSTER_SPIDER_WALK'); g_Sound_Delete('SOUND_MONSTER_FISH_ATTACK'); + + freeInds.Free(); + freeInds := nil; end; procedure g_Monsters_Init(); @@ -1179,16 +1275,21 @@ begin soulcount := 0; end; -procedure g_Monsters_Free(); +procedure g_Monsters_Free (clearGrid: Boolean=true); var a: Integer; begin - monsGrid.Free(); - monsGrid := nil; + e_LogWritefln('Cleared monster data (clearGrid=%s)', [clearGrid]); + if (clearGrid) then + begin + monsGrid.Free(); + monsGrid := nil; + end; for a := 0 to High(gMonsters) do gMonsters[a].Free(); gMonsters := nil; clearUidMap(); monCheckTrapLastFrameId := 0; + monCheckMPlatLastFrameId := 0; end; @@ -1197,6 +1298,8 @@ procedure g_Mons_InitTree (x, y, w, h: Integer); begin monsGrid.Free(); monsGrid := TMonsterGrid.Create(x, y, w, h); + //clearUidMap(); // why not? + e_LogWritefln('%s', ['Recreated monster tree']); end; @@ -1259,11 +1362,14 @@ begin if gMonsters = nil then Exit; -// Ïðèêîëèñò ñìååòñÿ íàä ñìåðòüþ èãðîêà: + // Ïðèêîëèñò ñìååòñÿ íàä ñìåðòüþ èãðîêà: h := High(gMonsters); for a := 0 to h do + begin if (gMonsters[a] <> nil) then + begin with gMonsters[a] do + begin if (FMonsterType = MONSTER_MAN) and (FState <> MONSTATE_DEAD) and (FState <> MONSTATE_SLEEP) and @@ -1272,6 +1378,9 @@ begin g_Sound_PlayExAt('SOUND_MONSTER_TRUP', FObj.X, FObj.Y); Exit; end; + end; + end; + end; end; procedure g_Monsters_Update(); @@ -1318,9 +1427,12 @@ var a: Integer; begin if gMonsters <> nil then + begin for a := 0 to High(gMonsters) do - if gMonsters[a] <> nil then - gMonsters[a].Draw(); + begin + if (gMonsters[a] <> nil) then gMonsters[a].Draw(); + end; + end; end; procedure g_Monsters_DrawHealth(); @@ -1332,142 +1444,179 @@ begin e_TextureFontGetSize(gStdFont, fW, fH); for a := 0 to High(gMonsters) do + begin if gMonsters[a] <> nil then begin e_TextureFontPrint(gMonsters[a].FObj.X + gMonsters[a].FObj.Rect.X, gMonsters[a].FObj.Y + gMonsters[a].FObj.Rect.Y + gMonsters[a].FObj.Rect.Height - fH, IntToStr(gMonsters[a].FHealth), gStdFont); end; + end; end; function g_Monsters_ByUID (UID: Word): TMonster; -//var a: Integer; begin result := uidMap[UID]; - { - Result := nil; - if gMonsters <> nil then - for a := 0 to High(gMonsters) do - if (gMonsters[a] <> nil) and - (gMonsters[a].FUID = UID) then - begin - Result := gMonsters[a]; - Break; - end; - } end; -procedure g_Monsters_SaveState(var Mem: TBinMemoryWriter); +procedure g_Monsters_SaveState (st: TStream); var count, i: Integer; - b: Byte; begin -// Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ìîíñòðîâ: + // Ñ÷èòàåì êîëè÷åñòâî ñóùåñòâóþùèõ ìîíñòðîâ count := 0; - if gMonsters <> nil then - for i := 0 to High(gMonsters) do - if gMonsters[i] <> nil then - if gMonsters[i].FMonsterType <> MONSTER_NONE then - count := count + 1; - - Mem := TBinMemoryWriter.Create((count+1) * 350); + for i := 0 to High(gMonsters) do + begin + if (gMonsters[i] <> nil) and (gMonsters[i].FMonsterType <> MONSTER_NONE) then count += 1; + end; -// Ñîõðàíÿåì èíôîðìàöèþ öåëåóêàçàòåëÿ: - Mem.WriteInt(pt_x); - Mem.WriteInt(pt_xs); - Mem.WriteInt(pt_y); - Mem.WriteInt(pt_ys); + // Ñîõðàíÿåì èíôîðìàöèþ öåëåóêàçàòåëÿ + utils.writeInt(st, LongInt(pt_x)); + utils.writeInt(st, LongInt(pt_xs)); + utils.writeInt(st, LongInt(pt_y)); + utils.writeInt(st, LongInt(pt_ys)); -// Êîëè÷åñòâî ìîíñòðîâ: - Mem.WriteInt(count); + // Êîëè÷åñòâî ìîíñòðîâ + utils.writeInt(st, LongInt(count)); - if count = 0 then - Exit; + if (count = 0) then exit; -// Ñîõðàíÿåì ìîíñòðîâ: + // Ñîõðàíÿåì ìîíñòðîâ for i := 0 to High(gMonsters) do - if gMonsters[i] <> nil then - if gMonsters[i].FMonsterType <> MONSTER_NONE then - begin - // Òèï ìîíñòðà: - b := gMonsters[i].MonsterType; - Mem.WriteByte(b); + begin + if (gMonsters[i] <> nil) and (gMonsters[i].FMonsterType <> MONSTER_NONE) then + begin + // Òèï ìîíñòðà + utils.writeInt(st, Byte(gMonsters[i].MonsterType)); // Ñîõðàíÿåì äàííûå ìîíñòðà: - gMonsters[i].SaveState(Mem); - end; + gMonsters[i].SaveState(st); + end; + end; end; -procedure g_Monsters_LoadState(var Mem: TBinMemoryReader); + +procedure g_Monsters_LoadState (st: TStream); var count, a: Integer; b: Byte; mon: TMonster; begin - if Mem = nil then exit; + assert(st <> nil); - g_Monsters_Free(); + g_Monsters_Free(false); // Çàãðóæàåì èíôîðìàöèþ öåëåóêàçàòåëÿ - Mem.ReadInt(pt_x); - Mem.ReadInt(pt_xs); - Mem.ReadInt(pt_y); - Mem.ReadInt(pt_ys); + pt_x := utils.readLongInt(st); + pt_xs := utils.readLongInt(st); + pt_y := utils.readLongInt(st); + pt_ys := utils.readLongInt(st); // Êîëè÷åñòâî ìîíñòðîâ - Mem.ReadInt(count); + count := utils.readLongInt(st); - if count = 0 then exit; + if (count = 0) then exit; + if (count < 0) or (count > 1024*1024) then raise XStreamError.Create('invalid monster count'); // Çàãðóæàåì ìîíñòðîâ for a := 0 to count-1 do begin // Òèï ìîíñòðà - Mem.ReadByte(b); + b := utils.readByte(st); // Ñîçäàåì ìîíñòðà - mon := g_Monsters_Create(b, 0, 0, D_LEFT); - if mon = nil then raise EBinSizeError.Create('g_Monsters_LoadState: ID = -1 (Can''t create)'); + mon := g_Monsters_Create(b, 0, 0, TDirection.D_LEFT); + if (mon = nil) then raise XStreamError.Create('g_Monsters_LoadState: ID = -1 (can''t create)'); // Çàãðóæàåì äàííûå ìîíñòðà - mon.LoadState(Mem); + mon.LoadState(st); end; end; -function g_Monsters_GetIDByName(name: String): Integer; + +// ////////////////////////////////////////////////////////////////////////// // +function g_Mons_SpawnAt (monType: Integer; x, y: Integer; dir: TDirection=TDirection.D_LEFT): TMonster; overload; +begin + result := nil; + if (monType >= MONSTER_DEMON) and (monType <= MONSTER_MAN) then + begin + result := g_Monsters_Create(monType, x, y, dir); + end; +end; + + +function g_Mons_SpawnAt (const typeName: AnsiString; x, y: Integer; dir: TDirection=TDirection.D_LEFT): TMonster; overload; +begin + result := g_Mons_SpawnAt(g_Mons_TypeIdByName(typeName), x, y, dir); +end; + + + +// ////////////////////////////////////////////////////////////////////////// // +function g_Mons_TypeLo (): Integer; inline; begin result := Low(MONSTERTABLE); end; +function g_Mons_TypeHi (): Integer; inline; begin result := High(MONSTERTABLE); end; + + +function g_Mons_TypeIdByName (const name: String): Integer; var i: Integer; begin - name := UpperCase(name); i := MONSTER_DEMON; while (i <= MONSTER_MAN) do begin - if name = MONSTERTABLE[i].Name then + if (CompareText(name, MONSTERTABLE[i].Name) = 0) then begin - Result := i; - Exit; + result := i; + exit; end; Inc(i); end; - - Result := -1; + result := -1; + // HACK! + if (CompareText(name, 'zombie') = 0) then result := MONSTER_ZOMBY; end; -function g_Monsters_GetNameByID(MonsterType: Byte): String; + +function g_Mons_NameByTypeId (monType: Integer): AnsiString; begin - if MonsterType in [MONSTER_DEMON..MONSTER_MAN] then - Result := MONSTERTABLE[MonsterType].Name + if (monType >= MONSTER_DEMON) and (monType <= MONSTER_MAN) then + result := MONSTERTABLE[monType].Name else - Result := '?'; + result := '?'; end; -function g_Monsters_GetKilledBy(MonsterType: Byte): String; + +function g_Mons_GetKilledByTypeId (monType: Integer): AnsiString; begin - if MonsterType in [MONSTER_DEMON..MONSTER_MAN] then - Result := KilledByMonster[MonsterType] + if (monType >= MONSTER_DEMON) and (monType <= MONSTER_MAN) then + Result := KilledByMonster[monType] else Result := '?'; end; + +// ////////////////////////////////////////////////////////////////////////// // { T M o n s t e r : } +procedure TMonster.setGameX (v: Integer); inline; begin FObj.X := v; positionChanged(); end; +procedure TMonster.setGameY (v: Integer); inline; begin FObj.Y := v; positionChanged(); end; + +procedure TMonster.setPosition (ax, ay: Integer; callPosChanged: Boolean=true); inline; begin FObj.X := ax; FObj.Y := ay; if callPosChanged then positionChanged(); end; + +procedure TMonster.moveBy (dx, dy: Integer); inline; +begin + if (dx <> 0) or (dy <> 0) then + begin + FObj.X += dx; + FObj.Y += dy; + positionChanged(); + end; +end; + +procedure TMonster.doDamage (v: Integer); +begin + if (v <= 0) then exit; + if (v > 32767) then v := 32767; + Damage(v, 0, 0, 0, mEDamageType); +end; + procedure TMonster.ActionSound(); begin case FMonsterType of @@ -1656,8 +1805,8 @@ begin FObj.Accel.X := 0; FObj.Accel.Y := 0; FDirection := FStartDirection; - GameX := FStartX; - GameY := FStartY; + {GameX}FObj.X := FStartX; + {GameY}FObj.Y := FStartY; FObj.Rect := MONSTERTABLE[FMonsterType].Rect; FHealth := MONSTERTABLE[FMonsterType].Health; FAmmo := 0; @@ -1713,10 +1862,13 @@ begin FFireTime := 0; FFirePainTime := 0; FFireAttacker := 0; + mEDamageType := HIT_SOME; mProxyId := -1; mArrIdx := -1; trapCheckFrameId := 0; + mplatCheckFrameId := 0; + mNeedSend := false; if FMonsterType in [MONSTER_ROBO, MONSTER_BARREL] then FBloodKind := BLOOD_SPARKS @@ -1745,8 +1897,8 @@ begin for a := 0 to High(FAnim) do begin - FAnim[a, D_LEFT] := nil; - FAnim[a, D_RIGHT] := nil; + FAnim[a, TDirection.D_LEFT] := nil; + FAnim[a, TDirection.D_RIGHT] := nil; end; for a := ANIM_SLEEP to ANIM_PAIN do @@ -1771,15 +1923,15 @@ begin if g_Frames_Get(FramesID, 'FRAMES_MONSTER_'+MONSTERTABLE[MonsterType].Name+ '_'+ANIMTABLE[ANIM_DIE].name) then begin - FAnim[a, D_RIGHT] := TAnimation.Create(FramesID, ANIMTABLE[ANIM_DIE].loop, + FAnim[a, TDirection.D_RIGHT] := TAnimation.Create(FramesID, ANIMTABLE[ANIM_DIE].loop, MONSTER_ANIMTABLE[MonsterType].AnimSpeed[ANIM_DIE]); - FAnim[a, D_LEFT] := TAnimation.Create(FramesID, ANIMTABLE[ANIM_DIE].loop, + FAnim[a, TDirection.D_LEFT] := TAnimation.Create(FramesID, ANIMTABLE[ANIM_DIE].loop, MONSTER_ANIMTABLE[MonsterType].AnimSpeed[ANIM_DIE]); Continue; end; end; - FAnim[a, D_RIGHT] := TAnimation.Create(FramesID, ANIMTABLE[a].loop, + FAnim[a, TDirection.D_RIGHT] := TAnimation.Create(FramesID, ANIMTABLE[a].loop, MONSTER_ANIMTABLE[MonsterType].AnimSpeed[a]); // Åñëè åñòü îòäåëüíàÿ ëåâàÿ àíèìàöèÿ - çàãðóæàåì: @@ -1791,7 +1943,7 @@ begin g_Frames_Get(FramesID, s); end; - FAnim[a, D_LEFT] := TAnimation.Create(FramesID, ANIMTABLE[a].loop, + FAnim[a, TDirection.D_LEFT] := TAnimation.Create(FramesID, ANIMTABLE[a].loop, MONSTER_ANIMTABLE[MonsterType].AnimSpeed[a]); end; @@ -1820,10 +1972,7 @@ begin if (t = HIT_ELECTRO) and (FMonsterType = MONSTER_FISH) and g_Game_IsServer then begin FSleep := 20; - if Random(2) = 0 then - FDirection := D_RIGHT - else - FDirection := D_LEFT; + if Random(2) = 0 then FDirection := TDirection.D_RIGHT else FDirection := TDirection.D_LEFT; Result := True; SetState(MONSTATE_RUN); Exit; @@ -1914,9 +2063,10 @@ begin it := g_Items_Create(FObj.X + (FObj.Rect.Width div 2), FObj.Y + (FObj.Rect.Height div 2), c, True, False); + g_Items_SetDrop(it); // mark it as monster drop g_Obj_Push(g_Items_ObjByIdx(it), (FObj.Vel.X div 2)-3+Random(7), (FObj.Vel.Y div 2)-Random(4)); - positionChanged(); // this updates spatial accelerators + //positionChanged(); // this updates spatial accelerators if g_Game_IsServer and g_Game_IsNet then MH_SEND_ItemSpawn(True, it); end; @@ -1929,6 +2079,7 @@ begin begin FObj.Rect.Y := FObj.Rect.Y + FObj.Rect.Height-12; FObj.Rect.Height := 12; + positionChanged(); end; // Óðîí áûë ñèëüíûì => ñëàáûå - â êàøó: @@ -1967,7 +2118,7 @@ begin Result := False; if g_Game_IsClient then Exit; - if not Live then + if not alive then Exit; if FHealth < FMaxHealth then @@ -1984,8 +2135,8 @@ var begin for a := 0 to High(FAnim) do begin - FAnim[a, D_LEFT].Free(); - FAnim[a, D_RIGHT].Free(); + FAnim[a, TDirection.D_LEFT].Free(); + FAnim[a, TDirection.D_RIGHT].Free(); end; vilefire.Free(); @@ -1996,13 +2147,17 @@ begin begin monsGrid.removeBody(mProxyId); {$IF DEFINED(D2F_DEBUG_MONS_MOVE)} - e_WriteLog(Format('monster #%d(%u): removed from tree; mProxyid=%d', [mArrIdx, UID, mProxyId]), MSG_NOTIFY); + e_WriteLog(Format('monster #%d:(%u): removed from grid; mProxyid=%d', [mArrIdx, UID, mProxyId]), MSG_NOTIFY); {$ENDIF} end; mProxyId := -1; end; - if (mArrIdx <> -1) and (mArrIdx < Length(gMonsters)) then gMonsters[mArrIdx] := nil; + if (mArrIdx <> -1) and (mArrIdx < Length(gMonsters)) then + begin + freeInds.release(mArrIdx); + gMonsters[mArrIdx] := nil; + end; mArrIdx := -1; uidMap[FUID] := nil; @@ -2024,12 +2179,16 @@ begin if FState = MONSTATE_SHOOT then if GetPos(FTargetUID, @o) then vilefire.Draw(o.X+o.Rect.X+(o.Rect.Width div 2)-32, - o.Y+o.Rect.Y+o.Rect.Height-128, M_NONE); + o.Y+o.Rect.Y+o.Rect.Height-128, TMirrorType.None); // Íå â îáëàñòè ðèñîâàíèÿ íå ðåñóåì: - if not g_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height, - sX-128, sY-128, sWidth+256, sHeight+256) then - Exit; +//FIXME! + if (g_dbg_scale = 1.0) then + begin + if not g_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y, FObj.Rect.Width, FObj.Rect.Height, + sX-128, sY-128, sWidth+256, sHeight+256) then + Exit; + end; // Ýòè ìîíñòðû, óìèðàÿ, íå îñòàâëÿþò òðóïîâ: if FState = MONSTATE_DEAD then @@ -2041,22 +2200,22 @@ begin if FAnim[FCurAnim, FDirection] <> nil then begin // Åñëè íåò ëåâîé àíèìàöèè èëè îíà ñîâïàäàåò ñ ïðàâîé => îòðàæàåì ïðàâóþ: - if (FDirection = D_LEFT) and + if (FDirection = TDirection.D_LEFT) and ((not MONSTER_ANIMTABLE[FMonsterType].LeftAnim) or - (FAnim[FCurAnim, D_LEFT].FramesID = FAnim[FCurAnim, D_RIGHT].FramesID)) and + (FAnim[FCurAnim, TDirection.D_LEFT].FramesID = FAnim[FCurAnim, TDirection.D_RIGHT].FramesID)) and (FMonsterType <> MONSTER_BARREL) then - m := M_HORIZONTAL + m := TMirrorType.Horizontal else - m := M_NONE; + m := TMirrorType.None; // Ëåâàÿ àíèìàöèÿ => ìåíÿåì ñìåùåíèå îòíîñèòåëüíî öåíòðà: - if (FDirection = D_LEFT) and + if (FDirection = TDirection.D_LEFT) and (FMonsterType <> MONSTER_BARREL) then begin dx := MONSTER_ANIMTABLE[FMonsterType].AnimDeltaLeft[FCurAnim].X; dy := MONSTER_ANIMTABLE[FMonsterType].AnimDeltaLeft[FCurAnim].Y; - if m = M_HORIZONTAL then + if m = TMirrorType.Horizontal then begin // Íåò îòäåëüíîé ëåâîé àíèìàöèè // Ðàññòîÿíèå îò êðàÿ òåêñòóðû äî êðàÿ âèçóàëüíîãî ïîëîæåíèÿ îáúåêòà íà òåêñòóðå: c := (MONSTERTABLE[FMonsterType].Rect.X - dx) + MONSTERTABLE[FMonsterType].Rect.Width; @@ -2209,17 +2368,17 @@ begin positionChanged(); if dir = 1 then - FDirection := D_LEFT + FDirection := TDirection.D_LEFT else if dir = 2 then - FDirection := D_RIGHT + FDirection := TDirection.D_RIGHT else if dir = 3 then begin // îáðàòíîå - if FDirection = D_RIGHT then - FDirection := D_LEFT + if FDirection = TDirection.D_RIGHT then + FDirection := TDirection.D_LEFT else - FDirection := D_RIGHT; + FDirection := TDirection.D_RIGHT; end; // Ýôôåêò òåëåïîðòà â òî÷êå íàçíà÷åíèÿ: @@ -2287,7 +2446,7 @@ begin (FObj.X > gMapInfo.Width+1000) or (FObj.Y < -1000) then begin FRemoved := True; - if Live and (gLMSRespawn = LMS_RESPAWN_NONE) then + if alive and (gLMSRespawn = LMS_RESPAWN_NONE) then begin Inc(gCoopMonstersKilled); if g_Game_IsNet then @@ -2450,7 +2609,7 @@ begin // Åñëè åñòü èãðîê ðÿäîì, ïðîñûïàåìñÿ è èäåì ê íåìó: if (gPlayers <> nil) then for a := 0 to High(gPlayers) do - if (gPlayers[a] <> nil) and (gPlayers[a].Live) + if (gPlayers[a] <> nil) and (gPlayers[a].alive) and (not gPlayers[a].NoTarget) and (gPlayers[a].FMegaRulez[MR_INVIS] < gTime) then with gPlayers[a] do if g_Look(@FObj, @Obj, FDirection) then @@ -2468,7 +2627,7 @@ begin // Åñëè åñòü ïîäõîäÿùèé ìîíñòð ðÿäîì: if gMonsters <> nil then for a := 0 to High(gMonsters) do - if (gMonsters[a] <> nil) and (gMonsters[a].Live) and + if (gMonsters[a] <> nil) and (gMonsters[a].alive) and (gMonsters[a].FUID <> FUID) then begin // Ìàíüÿêè íàïàäàþò íà âñåõ ìîíñòðîâ, êðîìå äðóçåé @@ -2558,9 +2717,9 @@ begin // Ïîâîðà÷èâàåìñÿ â ñòîðîíó öåëè: if sx > 0 then - FDirection := D_RIGHT + FDirection := TDirection.D_RIGHT else - FDirection := D_LEFT; + FDirection := TDirection.D_LEFT; // Åñëè ìîíñòð óìååò ñòðåëÿòü è åñòü ïî êîìó - ñòðåëÿåì: if canShoot(FMonsterType) and (FTargetUID <> 0) then @@ -2575,9 +2734,9 @@ begin FSleep := 15; SetState(MONSTATE_RUN); if Random(2) = 0 then - FDirection := D_LEFT + FDirection := TDirection.D_LEFT else - FDirection := D_RIGHT; + FDirection := TDirection.D_RIGHT; goto _end; end; @@ -2644,9 +2803,9 @@ begin FObj.Vel.Y := 0; // Ïëàâàåì òóäà-ñþäà: if Random(2) = 0 then - FDirection := D_LEFT + FDirection := TDirection.D_LEFT else - FDirection := D_RIGHT; + FDirection := TDirection.D_RIGHT; FSleep := 20; SetState(MONSTATE_RUN); end; @@ -2670,7 +2829,7 @@ begin if b > 1 then b := b * (Random(8 div b) + 1); for a := 0 to High(gGibs) do begin - if gGibs[a].Live and + if gGibs[a].alive and g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y+FObj.Rect.Height-4, FObj.Rect.Width, 8, @gGibs[a].Obj) and (Random(3) = 0) then begin @@ -2724,7 +2883,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -2765,7 +2924,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -2800,7 +2959,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -2832,7 +2991,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -2922,7 +3081,7 @@ _end: if (FMonsterType = MONSTER_PAIN) then begin mon := g_Monsters_Create(MONSTER_SOUL, FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2)-30, - FObj.Y+FObj.Rect.Y+20, D_LEFT); + FObj.Y+FObj.Rect.Y+20, TDirection.D_LEFT); if mon <> nil then begin mon.SetState(MONSTATE_GO); @@ -2932,7 +3091,7 @@ _end: end; mon := g_Monsters_Create(MONSTER_SOUL, FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2), - FObj.Y+FObj.Rect.Y+20, D_RIGHT); + FObj.Y+FObj.Rect.Y+20, TDirection.D_RIGHT); if mon <> nil then begin mon.SetState(MONSTATE_GO); @@ -2942,7 +3101,7 @@ _end: end; mon := g_Monsters_Create(MONSTER_SOUL, FObj.X+FObj.Rect.X+(FObj.Rect.Width div 2)-15, - FObj.Y+FObj.Rect.Y, D_RIGHT); + FObj.Y+FObj.Rect.Y, TDirection.D_RIGHT); if mon <> nil then begin mon.SetState(MONSTATE_GO); @@ -3021,7 +3180,7 @@ _end: if FCurAnim = ANIM_ATTACK2 then begin o := FObj; - o.Vel.X := IfThen(FDirection = D_RIGHT, 1, -1)*IfThen(FMonsterType = MONSTER_CYBER, 60, 50); + o.Vel.X := IfThen(FDirection = TDirection.D_RIGHT, 1, -1)*IfThen(FMonsterType = MONSTER_CYBER, 60, 50); if g_Weapon_Hit(@o, IfThen(FMonsterType = MONSTER_CYBER, 33, 50), FUID, HIT_SOME) <> 0 then g_Sound_PlayExAt('SOUND_MONSTER_SKEL_HIT', FObj.X, FObj.Y); end; @@ -3047,7 +3206,7 @@ _end: // Âû÷èñëÿåì êîîðäèíàòû, îòêóäà âûëåòèò ïóëÿ: wx := MONSTER_ANIMTABLE[FMonsterType].wX; - if FDirection = D_LEFT then + if FDirection = TDirection.D_LEFT then begin wx := MONSTER_ANIMTABLE[FMonsterType].wX-(MONSTERTABLE[FMonsterType].Rect.X+(MONSTERTABLE[FMonsterType].Rect.Width div 2)); wx := MONSTERTABLE[FMonsterType].Rect.X+(MONSTERTABLE[FMonsterType].Rect.Width div 2)-wx; @@ -3502,7 +3661,7 @@ begin if b > 1 then b := b * (Random(8 div b) + 1); for a := 0 to High(gGibs) do begin - if gGibs[a].Live and + if gGibs[a].alive and g_Obj_Collide(FObj.X+FObj.Rect.X, FObj.Y+FObj.Rect.Y+FObj.Rect.Height-4, FObj.Rect.Width, 8, @gGibs[a].Obj) and (Random(3) = 0) then begin @@ -3551,7 +3710,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -3589,7 +3748,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -3622,7 +3781,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -3653,7 +3812,7 @@ begin end; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -3801,7 +3960,7 @@ _end: if FCurAnim = ANIM_ATTACK2 then begin o := FObj; - o.Vel.X := IfThen(FDirection = D_RIGHT, 1, -1)*IfThen(FMonsterType = MONSTER_CYBER, 60, 50); + o.Vel.X := IfThen(FDirection = TDirection.D_RIGHT, 1, -1)*IfThen(FMonsterType = MONSTER_CYBER, 60, 50); g_Weapon_Hit(@o, IfThen(FMonsterType = MONSTER_CYBER, 33, 50), FUID, HIT_SOME); end; @@ -3922,13 +4081,10 @@ end; procedure TMonster.Turn(); begin // Ðàçâîðà÷èâàåìñÿ: - if FDirection = D_LEFT then - FDirection := D_RIGHT - else - FDirection := D_LEFT; + if FDirection = TDirection.D_LEFT then FDirection := TDirection.D_RIGHT else FDirection := TDirection.D_LEFT; // Áåæèì â âûáðàííóþ ñòîðîíó: - if FDirection = D_RIGHT then + if FDirection = TDirection.D_RIGHT then FObj.Vel.X := MONSTERTABLE[FMonsterType].RunVel else FObj.Vel.X := -MONSTERTABLE[FMonsterType].RunVel; @@ -3954,7 +4110,7 @@ begin if (gPlayers <> nil) and (FBehaviour <> BH_INSANE) and (FBehaviour <> BH_CANNIBAL) and (FBehaviour <> BH_GOOD) then for a := 0 to High(gPlayers) do - if (gPlayers[a] <> nil) and (gPlayers[a].Live) + if (gPlayers[a] <> nil) and (gPlayers[a].alive) and (not gPlayers[a].NoTarget) and (gPlayers[a].FMegaRulez[MR_INVIS] < gTime) then begin if g_Look(@FObj, @gPlayers[a].Obj, FDirection) then @@ -3974,7 +4130,7 @@ begin // Êèëëåðû è äîáðûå íå òðîãàþò ìîíñòðîâ if (gMonsters <> nil) and (FBehaviour <> BH_KILLER) and (FBehaviour <> BH_GOOD) then for a := 0 to High(gMonsters) do - if (gMonsters[a] <> nil) and (gMonsters[a].Live) and + if (gMonsters[a] <> nil) and (gMonsters[a].alive) and (gMonsters[a].FUID <> FUID) then begin if (FBehaviour = BH_CANNIBAL) and (gMonsters[a].FMonsterType <> FMonsterType) then @@ -4227,7 +4383,7 @@ begin Result := True; end; -function TMonster.Live(): Boolean; +function TMonster.alive(): Boolean; begin Result := (FState <> MONSTATE_DIE) and (FState <> MONSTATE_DEAD) and (FHealth > 0); end; @@ -4250,181 +4406,170 @@ begin WakeUpSound(); end; -procedure TMonster.SaveState(var Mem: TBinMemoryWriter); +procedure TMonster.SaveState (st: TStream); var i: Integer; - sig: DWORD; b: Byte; anim: Boolean; begin - if Mem = nil then - Exit; - -// Ñèãíàòóðà ìîíñòðà: - sig := MONSTER_SIGNATURE; // 'MONS' - Mem.WriteDWORD(sig); -// UID ìîíñòðà: - Mem.WriteWord(FUID); -// Íàïðàâëåíèå: - if FDirection = D_LEFT then - b := 1 - else // D_RIGHT - b := 2; - Mem.WriteByte(b); -// Íàäî ëè óäàëèòü åãî: - Mem.WriteBoolean(FRemoved); -// Îñòàëîñü çäîðîâüÿ: - Mem.WriteInt(FHealth); -// Ñîñòîÿíèå: - Mem.WriteByte(FState); -// Òåêóùàÿ àíèìàöèÿ: - Mem.WriteByte(FCurAnim); -// UID öåëè: - Mem.WriteWord(FTargetUID); -// Âðåìÿ ïîñëå ïîòåðè öåëè: - Mem.WriteInt(FTargetTime); -// Ïîâåäåíèå ìîíñòðà: - Mem.WriteByte(FBehaviour); -// Ãîòîâíîñòü ê âûñòðåëó: - Mem.WriteInt(FAmmo); -// Áîëü: - Mem.WriteInt(FPain); -// Âðåìÿ îæèäàíèÿ: - Mem.WriteInt(FSleep); -// Îçâó÷èâàòü ëè áîëü: - Mem.WriteBoolean(FPainSound); -// Áûëà ëè àòàêà âî âðåìÿ àíèìàöèè àòàêè: - Mem.WriteBoolean(FWaitAttackAnim); -// Íàäî ëè ñòðåëÿòü íà ñëåäóþùåì øàãå: - Mem.WriteBoolean(FChainFire); -// Ïîäëåæèò ëè ðåñïàâíó: - Mem.WriteBoolean(FNoRespawn); -// Êîîðäèíàòû öåëè: - Mem.WriteInt(tx); - Mem.WriteInt(ty); -// ID ìîíñòðà ïðè ñòàðòå êàðòû: - Mem.WriteInt(FStartID); -// Èíäåêñ òðèããåðà, ñîçäàâøåãî ìîíñòðà: - Mem.WriteInt(FSpawnTrigger); -// Îáúåêò ìîíñòðà: - Obj_SaveState(@FObj, Mem); -// Åñòü ëè àíèìàöèÿ îãíÿ êîëäóíà: - anim := vilefire <> nil; - Mem.WriteBoolean(anim); -// Åñëè åñòü - ñîõðàíÿåì: - if anim then - vilefire.SaveState(Mem); -// Àíèìàöèè: + assert(st <> nil); + + // Ñèãíàòóðà ìîíñòðà: + utils.writeSign(st, 'MONS'); + utils.writeInt(st, Byte(0)); // version + // UID ìîíñòðà: + utils.writeInt(st, Word(FUID)); + // Íàïðàâëåíèå + if FDirection = TDirection.D_LEFT then b := 1 else b := 2; // D_RIGHT + utils.writeInt(st, Byte(b)); + // Íàäî ëè óäàëèòü åãî + utils.writeBool(st, FRemoved); + // Îñòàëîñü çäîðîâüÿ + utils.writeInt(st, LongInt(FHealth)); + // Ñîñòîÿíèå + utils.writeInt(st, Byte(FState)); + // Òåêóùàÿ àíèìàöèÿ + utils.writeInt(st, Byte(FCurAnim)); + // UID öåëè + utils.writeInt(st, Word(FTargetUID)); + // Âðåìÿ ïîñëå ïîòåðè öåëè + utils.writeInt(st, LongInt(FTargetTime)); + // Ïîâåäåíèå ìîíñòðà + utils.writeInt(st, Byte(FBehaviour)); + // Ãîòîâíîñòü ê âûñòðåëó + utils.writeInt(st, LongInt(FAmmo)); + // Áîëü + utils.writeInt(st, LongInt(FPain)); + // Âðåìÿ îæèäàíèÿ + utils.writeInt(st, LongInt(FSleep)); + // Îçâó÷èâàòü ëè áîëü + utils.writeBool(st, FPainSound); + // Áûëà ëè àòàêà âî âðåìÿ àíèìàöèè àòàêè + utils.writeBool(st, FWaitAttackAnim); + // Íàäî ëè ñòðåëÿòü íà ñëåäóþùåì øàãå + utils.writeBool(st, FChainFire); + // Ïîäëåæèò ëè ðåñïàâíó + utils.writeBool(st, FNoRespawn); + // Êîîðäèíàòû öåëè + utils.writeInt(st, LongInt(tx)); + utils.writeInt(st, LongInt(ty)); + // ID ìîíñòðà ïðè ñòàðòå êàðòû + utils.writeInt(st, LongInt(FStartID)); + // Èíäåêñ òðèããåðà, ñîçäàâøåãî ìîíñòðà + utils.writeInt(st, LongInt(FSpawnTrigger)); + // Îáúåêò ìîíñòðà + Obj_SaveState(st, @FObj); + // Åñòü ëè àíèìàöèÿ îãíÿ êîëäóíà + anim := (vilefire <> nil); + utils.writeBool(st, anim); + // Åñëè åñòü - ñîõðàíÿåì: + if anim then vilefire.SaveState(st); + // Àíèìàöèè for i := ANIM_SLEEP to ANIM_PAIN do begin - // Åñòü ëè ëåâàÿ àíèìàöèÿ: - anim := FAnim[i, D_LEFT] <> nil; - Mem.WriteBoolean(anim); - // Åñëè åñòü - ñîõðàíÿåì: - if anim then - FAnim[i, D_LEFT].SaveState(Mem); - // Åñòü ëè ïðàâàÿ àíèìàöèÿ: - anim := FAnim[i, D_RIGHT] <> nil; - Mem.WriteBoolean(anim); - // Åñëè åñòü - ñîõðàíÿåì: - if anim then - FAnim[i, D_RIGHT].SaveState(Mem); + // Åñòü ëè ëåâàÿ àíèìàöèÿ + anim := (FAnim[i, TDirection.D_LEFT] <> nil); + utils.writeBool(st, anim); + // Åñëè åñòü - ñîõðàíÿåì + if anim then FAnim[i, TDirection.D_LEFT].SaveState(st); + // Åñòü ëè ïðàâàÿ àíèìàöèÿ + anim := (FAnim[i, TDirection.D_RIGHT] <> nil); + utils.writeBool(st, anim); + // Åñëè åñòü - ñîõðàíÿåì + if anim then FAnim[i, TDirection.D_RIGHT].SaveState(st); end; end; -procedure TMonster.LoadState(var Mem: TBinMemoryReader); + +procedure TMonster.LoadState (st: TStream); var i: Integer; - sig: DWORD; b: Byte; anim: Boolean; begin - if Mem = nil then - Exit; + assert(st <> nil); -// Ñèãíàòóðà ìîíñòðà: - Mem.ReadDWORD(sig); - if sig <> MONSTER_SIGNATURE then // 'MONS' - begin - raise EBinSizeError.Create('TMonster.LoadState: Wrong Monster Signature'); - end; -// UID ìîíñòðà: - Mem.ReadWord(FUID); -// Íàïðàâëåíèå: - Mem.ReadByte(b); - if b = 1 then - FDirection := D_LEFT - else // b = 2 - FDirection := D_RIGHT; -// Íàäî ëè óäàëèòü åãî: - Mem.ReadBoolean(FRemoved); -// Îñòàëîñü çäîðîâüÿ: - Mem.ReadInt(FHealth); -// Ñîñòîÿíèå: - Mem.ReadByte(FState); -// Òåêóùàÿ àíèìàöèÿ: - Mem.ReadByte(FCurAnim); -// UID öåëè: - Mem.ReadWord(FTargetUID); -// Âðåìÿ ïîñëå ïîòåðè öåëè: - Mem.ReadInt(FTargetTime); -// Ïîâåäåíèå ìîíñòðà: - Mem.ReadByte(FBehaviour); -// Ãîòîâíîñòü ê âûñòðåëó: - Mem.ReadInt(FAmmo); -// Áîëü: - Mem.ReadInt(FPain); -// Âðåìÿ îæèäàíèÿ: - Mem.ReadInt(FSleep); -// Îçâó÷èâàòü ëè áîëü: - Mem.ReadBoolean(FPainSound); -// Áûëà ëè àòàêà âî âðåìÿ àíèìàöèè àòàêè: - Mem.ReadBoolean(FWaitAttackAnim); -// Íàäî ëè ñòðåëÿòü íà ñëåäóþùåì øàãå: - Mem.ReadBoolean(FChainFire); -// Ïîäëåæèò ëè ðåñïàâíó - Mem.ReadBoolean(FNoRespawn); -// Êîîðäèíàòû öåëè: - Mem.ReadInt(tx); - Mem.ReadInt(ty); -// ID ìîíñòðà ïðè ñòàðòå êàðòû: - Mem.ReadInt(FStartID); -// Èíäåêñ òðèããåðà, ñîçäàâøåãî ìîíñòðà: - Mem.ReadInt(FSpawnTrigger); -// Îáúåêò ìîíñòðà: - Obj_LoadState(@FObj, Mem); -// Åñòü ëè àíèìàöèÿ îãíÿ êîëäóíà: - Mem.ReadBoolean(anim); -// Åñëè åñòü - çàãðóæàåì: + // Ñèãíàòóðà ìîíñòðà: + if not utils.checkSign(st, 'MONS') then raise XStreamError.Create('invalid monster signature'); + if (utils.readByte(st) <> 0) then raise XStreamError.Create('invalid monster version'); + if (uidMap[FUID] <> nil) and (uidMap[FUID] <> self) then raise Exception.Create('internal error in monster loader (0)'); + uidMap[FUID] := nil; + // UID ìîíñòðà: + FUID := utils.readWord(st); + //if (arrIdx = -1) then raise Exception.Create('internal error in monster loader'); + if (uidMap[FUID] <> nil) then raise Exception.Create('internal error in monster loader (1)'); + uidMap[FUID] := self; + // Íàïðàâëåíèå + b := utils.readByte(st); + if b = 1 then FDirection := TDirection.D_LEFT else FDirection := TDirection.D_RIGHT; // b = 2 + // Íàäî ëè óäàëèòü åãî + FRemoved := utils.readBool(st); + // Îñòàëîñü çäîðîâüÿ + FHealth := utils.readLongInt(st); + // Ñîñòîÿíèå + FState := utils.readByte(st); + // Òåêóùàÿ àíèìàöèÿ + FCurAnim := utils.readByte(st); + // UID öåëè + FTargetUID := utils.readWord(st); + // Âðåìÿ ïîñëå ïîòåðè öåëè + FTargetTime := utils.readLongInt(st); + // Ïîâåäåíèå ìîíñòðà + FBehaviour := utils.readByte(st); + // Ãîòîâíîñòü ê âûñòðåëó + FAmmo := utils.readLongInt(st); + // Áîëü + FPain := utils.readLongInt(st); + // Âðåìÿ îæèäàíèÿ + FSleep := utils.readLongInt(st); + // Îçâó÷èâàòü ëè áîëü + FPainSound := utils.readBool(st); + // Áûëà ëè àòàêà âî âðåìÿ àíèìàöèè àòàêè + FWaitAttackAnim := utils.readBool(st); + // Íàäî ëè ñòðåëÿòü íà ñëåäóþùåì øàãå + FChainFire := utils.readBool(st); + // Ïîäëåæèò ëè ðåñïàâíó + FNoRespawn := utils.readBool(st); + // Êîîðäèíàòû öåëè + tx := utils.readLongInt(st); + ty := utils.readLongInt(st); + // ID ìîíñòðà ïðè ñòàðòå êàðòû + FStartID := utils.readLongInt(st); + // Èíäåêñ òðèããåðà, ñîçäàâøåãî ìîíñòðà + FSpawnTrigger := utils.readLongInt(st); + // Îáúåêò ìîíñòðà + Obj_LoadState(@FObj, st); + // Åñòü ëè àíèìàöèÿ îãíÿ êîëäóíà + anim := utils.readBool(st); + // Åñëè åñòü - çàãðóæàåì: if anim then begin Assert(vilefire <> nil, 'TMonster.LoadState: no vilefire anim'); - vilefire.LoadState(Mem); + vilefire.LoadState(st); end; -// Àíèìàöèè: + // Àíèìàöèè for i := ANIM_SLEEP to ANIM_PAIN do begin - // Åñòü ëè ëåâàÿ àíèìàöèÿ: - Mem.ReadBoolean(anim); - // Åñëè åñòü - çàãðóæàåì: + // Åñòü ëè ëåâàÿ àíèìàöèÿ + anim := utils.readBool(st); + // Åñëè åñòü - çàãðóæàåì if anim then begin - Assert(FAnim[i, D_LEFT] <> nil, - 'TMonster.LoadState: no '+IntToStr(i)+'_left anim'); - FAnim[i, D_LEFT].LoadState(Mem); + Assert(FAnim[i, TDirection.D_LEFT] <> nil, 'TMonster.LoadState: no '+IntToStr(i)+'_left anim'); + FAnim[i, TDirection.D_LEFT].LoadState(st); end; - // Åñòü ëè ïðàâàÿ àíèìàöèÿ: - Mem.ReadBoolean(anim); - // Åñëè åñòü - çàãðóæàåì: + // Åñòü ëè ïðàâàÿ àíèìàöèÿ + anim := utils.readBool(st); + // Åñëè åñòü - çàãðóæàåì if anim then begin - Assert(FAnim[i, D_RIGHT] <> nil, - 'TMonster.LoadState: no '+IntToStr(i)+'_right anim'); - FAnim[i, D_RIGHT].LoadState(Mem); + Assert(FAnim[i, TDirection.D_RIGHT] <> nil, 'TMonster.LoadState: no '+IntToStr(i)+'_right anim'); + FAnim[i, TDirection.D_RIGHT].LoadState(st); end; end; end; + procedure TMonster.ActivateTriggers(); var a: Integer; @@ -4529,7 +4674,7 @@ begin for idx := 0 to High(gMonsters) do begin mon := gMonsters[idx]; - if (mon <> nil) and mon.Live then + if (mon <> nil) and mon.alive then begin result := cb(mon); if result then exit; @@ -4542,7 +4687,7 @@ function g_Mons_IsAnyAliveAt (x, y: Integer; width, height: Integer): Boolean; function monsCollCheck (mon: TMonster; atag: Integer): Boolean; begin - result := mon.Live;// and g_Obj_Collide(x, y, width, height, @mon.Obj)); + result := mon.alive;// and g_Obj_Collide(x, y, width, height, @mon.Obj)); end; var @@ -4560,7 +4705,7 @@ begin for idx := 0 to High(gMonsters) do begin mon := gMonsters[idx]; - if (mon <> nil) and mon.Live then + if (mon <> nil) and mon.alive then begin if g_Obj_Collide(x, y, width, height, @mon.Obj) then begin @@ -4595,7 +4740,7 @@ begin for idx := 0 to High(gMonsters) do begin mon := gMonsters[idx]; - if (mon <> nil) and mon.Live then + if (mon <> nil) and mon.alive then begin if g_Obj_Collide(x, y, width, height, @mon.Obj) then begin @@ -4613,8 +4758,8 @@ function g_Mons_ForEachAliveAt (x, y: Integer; width, height: Integer; cb: TEach function monsCollCheck (mon: TMonster; atag: Integer): Boolean; begin //result := false; - //if mon.Live and g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon); - if mon.Live then result := cb(mon) else result := false; + //if mon.alive and g_Obj_Collide(x, y, width, height, @mon.Obj) then result := cb(mon); + if mon.alive then result := cb(mon) else result := false; end; var @@ -4639,7 +4784,7 @@ begin for idx := 0 to High(gMonsters) do begin mon := gMonsters[idx]; - if (mon <> nil) and mon.Live then + if (mon <> nil) and mon.alive then begin if g_Obj_Collide(x, y, width, height, @mon.Obj) then begin