DEADSOFTWARE

some new code for moving platforms: only for players yet
[d2df-sdl.git] / src / game / g_monsters.pas
index 5ec0099df31ef59bf398bb2c52b229cba0a5dc31..62686cd4409d24785029888d2809c4feba7e467b 100644 (file)
@@ -14,6 +14,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
 {$INCLUDE ../shared/a_modes.inc}
+{$M+}
 {.$DEFINE D2F_DEBUG_MONS_MOVE}
 unit g_monsters;
 
@@ -90,6 +91,9 @@ type
     function findNewPrey(): Boolean;
     procedure ActivateTriggers();
 
+    procedure setGameX (v: Integer); inline;
+    procedure setGameY (v: Integer); inline;
+
   public
     FNoRespawn: Boolean;
     FFireTime: Integer;
@@ -101,7 +105,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;
@@ -134,8 +138,17 @@ 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 getMapBox (out x, y, w, h: Integer); inline;
 
+  public
+    property Obj: TObj read FObj;
+
+    property proxyId: Integer read mProxyId;
+    property arrIdx: Integer read mArrIdx;
+
+  published
     property MonsterType: Byte read FMonsterType;
     property MonsterHealth: Integer read FHealth write FHealth;
     property MonsterAmmo: Integer read FAmmo write FAmmo;
@@ -148,12 +161,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,9 +173,6 @@ type
     property GameDirection: TDirection read FDirection write FDirection;
 
     property StartID: Integer read FStartID;
-
-    property proxyId: Integer read mProxyId;
-    property arrIdx: Integer read mArrIdx;
   end;
 
 
@@ -183,9 +192,16 @@ 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;
+
+function g_Mons_SpawnAt (monType: Integer; x, y: Integer; dir: TDirection=D_LEFT): TMonster; overload;
+function g_Mons_SpawnAt (const typeName: AnsiString; x, y: Integer; dir: 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
@@ -252,6 +268,7 @@ uses
   g_language, g_netmsg, idpool;
 
 
+
 // ////////////////////////////////////////////////////////////////////////// //
 procedure g_Mons_ProfilersBegin ();
 begin
@@ -539,6 +556,7 @@ const
   MAX_SOUL = 512; // Îãðàíè÷åíèå Lost_Soul'îâ
 
 
+// ////////////////////////////////////////////////////////////////////////// //
 var
   gMonsters: array of TMonster;
   uidMap: array [0..65535] of TMonster; // monster knows it's index
@@ -1450,43 +1468,76 @@ begin
   end;
 end;
 
-function g_Monsters_GetIDByName(name: String): Integer;
+
+// ////////////////////////////////////////////////////////////////////////// //
+function g_Mons_SpawnAt (monType: Integer; x, y: Integer; dir: 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=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.ActionSound();
 begin
   case FMonsterType of
@@ -1675,8 +1726,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;
@@ -1987,7 +2038,7 @@ begin
   Result := False;
   if g_Game_IsClient then
     Exit;
-  if not Live then
+  if not alive then
     Exit;
 
   if FHealth < FMaxHealth then
@@ -2051,9 +2102,12 @@ begin
                       o.Y+o.Rect.Y+o.Rect.Height-128, M_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;
+  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
@@ -2311,7 +2365,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
@@ -2474,7 +2528,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
@@ -2492,7 +2546,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
                 // Ìàíüÿêè íàïàäàþò íà âñåõ ìîíñòðîâ, êðîìå äðóçåé
@@ -2694,7 +2748,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
@@ -3526,7 +3580,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
@@ -3978,7 +4032,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
@@ -3998,7 +4052,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
@@ -4251,7 +4305,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;
@@ -4559,7 +4613,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;
@@ -4572,7 +4626,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
@@ -4590,7 +4644,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
@@ -4625,7 +4679,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
@@ -4643,8 +4697,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
@@ -4669,7 +4723,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