DEADSOFTWARE

simplified trigger loading code
[d2df-sdl.git] / src / game / g_monsters.pas
index 62686cd4409d24785029888d2809c4feba7e467b..71619512a3648a3d69e88eb390f5a5f2f8941a20 100644 (file)
@@ -38,12 +38,14 @@ 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)
@@ -87,6 +89,8 @@ type
     FDieTriggers: Array of Integer;
     FSpawnTrigger: Integer;
 
+    mNeedSend: Boolean; // for network
+
     procedure Turn();
     function findNewPrey(): Boolean;
     procedure ActivateTriggers();
@@ -98,6 +102,7 @@ type
     FNoRespawn: Boolean;
     FFireTime: Integer;
     trapCheckFrameId: DWord; // for `g_weapons.CheckTrap()`
+    mplatCheckFrameId: LongWord;
 
     constructor Create(MonsterType: Byte; aID: Integer; ForcedUID: Integer = -1);
     destructor Destroy(); override;
@@ -139,9 +144,14 @@ 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;
 
@@ -223,7 +233,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
@@ -299,7 +310,8 @@ end;
 
 // ////////////////////////////////////////////////////////////////////////// //
 var
-  monCheckTrapLastFrameId: DWord;
+  monCheckTrapLastFrameId: DWord = 0;
+  monCheckMPlatLastFrameId: LongWord = 0;
 
 
 procedure TMonster.getMapBox (out x, y, w, h: Integer); inline;
@@ -310,6 +322,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;
@@ -330,6 +346,7 @@ begin
   {$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);
@@ -343,6 +360,7 @@ begin
 
     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}
@@ -350,6 +368,7 @@ begin
     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}
@@ -572,12 +591,12 @@ begin
 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;
@@ -590,6 +609,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;
@@ -976,6 +1013,7 @@ begin
   freeInds := TIdPool.Create();
   clearUidMap();
   monCheckTrapLastFrameId := 0;
+  monCheckMPlatLastFrameId := 0;
 end;
 
 procedure g_Monsters_FreeData();
@@ -1216,6 +1254,7 @@ begin
   gMonsters := nil;
   clearUidMap();
   monCheckTrapLastFrameId := 0;
+  monCheckMPlatLastFrameId := 0;
 end;
 
 
@@ -1535,8 +1574,19 @@ end;
 
 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.ActionSound();
 begin
@@ -1787,6 +1837,8 @@ begin
   mProxyId := -1;
   mArrIdx := -1;
   trapCheckFrameId := 0;
+  mplatCheckFrameId := 0;
+  mNeedSend := false;
 
   if FMonsterType in [MONSTER_ROBO, MONSTER_BARREL] then
     FBloodKind := BLOOD_SPARKS
@@ -2102,6 +2154,7 @@ begin
                       o.Y+o.Rect.Y+o.Rect.Height-128, M_NONE);
 
 // Íå â îáëàñòè ðèñîâàíèÿ íå ðåñóåì:
+//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,