From 91981e25a8dcdf83368965c28811f83a41d8d585 Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sat, 7 Jan 2017 20:02:25 +0100
Subject: [PATCH] - moved all 'sectorptr - &level.sectors[0]' constructs into a
 subfunction.

---
 src/edata.cpp            |  2 +-
 src/g_level.h            |  1 -
 src/gl/data/gl_setup.cpp |  2 +-
 src/p_effect.cpp         |  4 ++--
 src/p_mobj.cpp           |  4 ++--
 src/p_pusher.cpp         |  3 +--
 src/p_scroll.cpp         |  8 ++++----
 src/p_setup.cpp          |  4 ++--
 src/p_sight.cpp          |  2 +-
 src/p_spec.cpp           |  8 ++++----
 src/p_tags.cpp           | 11 +++--------
 src/p_tags.h             |  2 +-
 src/r_defs.h             |  2 +-
 13 files changed, 23 insertions(+), 30 deletions(-)

diff --git a/src/edata.cpp b/src/edata.cpp
index d9a62707c0..258c2a187d 100644
--- a/src/edata.cpp
+++ b/src/edata.cpp
@@ -764,7 +764,7 @@ void ProcessEDSectors()
 	{
 		if (lines[i].special == Static_Init && lines[i].args[1] == Init_EDSector)
 		{
-			sectorrecord[lines[i].frontsector - &level.sectors[0]] = lines[i].args[0];
+			sectorrecord[lines[i].frontsector->Index()] = lines[i].args[0];
 			lines[i].special = 0;
 		}
 	}
diff --git a/src/g_level.h b/src/g_level.h
index c31e11d6fd..5808df138a 100644
--- a/src/g_level.h
+++ b/src/g_level.h
@@ -448,7 +448,6 @@ struct FLevelLocals
 	bool		IsFreelookAllowed() const;
 };
 
-
 struct cluster_info_t
 {
 	int			cluster;
diff --git a/src/gl/data/gl_setup.cpp b/src/gl/data/gl_setup.cpp
index bebdb3e8a9..3fc022cf8e 100644
--- a/src/gl/data/gl_setup.cpp
+++ b/src/gl/data/gl_setup.cpp
@@ -381,7 +381,7 @@ static void PrepareTransparentDoors(sector_t * sector)
 
 static void AddToVertex(const sector_t * sec, TArray<int> & list)
 {
-	int secno = int(sec-&level.sectors[0]);
+	int secno = sec->Index();
 
 	for(unsigned i=0;i<list.Size();i++)
 	{
diff --git a/src/p_effect.cpp b/src/p_effect.cpp
index ad6b171ebd..1077594271 100644
--- a/src/p_effect.cpp
+++ b/src/p_effect.cpp
@@ -350,7 +350,7 @@ void P_RunEffects ()
 {
 	if (players[consoleplayer].camera == NULL) return;
 
-	int	pnum = int(players[consoleplayer].camera->Sector - &level.sectors[0]) * level.sectors.Size();
+	int	pnum = players[consoleplayer].camera->Sector->Index() * level.sectors.Size();
 
 	AActor *actor;
 	TThinkerIterator<AActor> iterator;
@@ -360,7 +360,7 @@ void P_RunEffects ()
 		if (actor->effects)
 		{
 			// Only run the effect if the actor is potentially visible
-			int rnum = pnum + int(actor->Sector - &level.sectors[0]);
+			int rnum = pnum + actor->Sector->Index();
 			if (rejectmatrix == NULL || !(rejectmatrix[rnum>>3] & (1 << (rnum & 7))))
 				P_RunEffect (actor, actor->effects);
 		}
diff --git a/src/p_mobj.cpp b/src/p_mobj.cpp
index 9ef5a6d55e..3bdf3718dc 100644
--- a/src/p_mobj.cpp
+++ b/src/p_mobj.cpp
@@ -3961,9 +3961,9 @@ void AActor::Tick ()
 				sector_t *sec = node->m_sector;
 				DVector2 scrollv;
 
-				if (level.Scrolls.Size() > unsigned(sec-&level.sectors[0]))
+				if (level.Scrolls.Size() > unsigned(sec->Index()))
 				{
-					scrollv = level.Scrolls[sec - &level.sectors[0]];
+					scrollv = level.Scrolls[sec->Index()];
 				}
 				else
 				{
diff --git a/src/p_pusher.cpp b/src/p_pusher.cpp
index b0f18c0b5d..8a4032b402 100644
--- a/src/p_pusher.cpp
+++ b/src/p_pusher.cpp
@@ -406,8 +406,7 @@ void P_SpawnPushers ()
 					if (thing->GetClass()->TypeName == NAME_PointPusher ||
 						thing->GetClass()->TypeName == NAME_PointPuller)
 					{
-						new DPusher (DPusher::p_push, l->args[3] ? l : NULL, l->args[2],
-									 0, thing, int(thing->Sector - &level.sectors[0]));
+						new DPusher (DPusher::p_push, l->args[3] ? l : NULL, l->args[2], 0, thing, thing->Sector->Index());
 					}
 				}
 			}
diff --git a/src/p_scroll.cpp b/src/p_scroll.cpp
index d2e8efa3e5..954b1ac368 100644
--- a/src/p_scroll.cpp
+++ b/src/p_scroll.cpp
@@ -441,7 +441,7 @@ void P_SpawnScrollers(void)
 			{
 				// if 1, then displacement
 				// if 2, then accelerative (also if 3)
-				control = int(l->sidedef[0]->sector - &level.sectors[0]);
+				control = l->sidedef[0]->sector->Index();
 				if (l->args[1] & 2)
 					accel = 1;
 			}
@@ -478,7 +478,7 @@ void P_SpawnScrollers(void)
 
 				if (line->args[0] == l->args[0] && (line->args[1] & 1))
 				{
-					new DScroller(EScroll::sc_ceiling, -dx, dy, control, int(line->frontsector - &level.sectors[0]), accel);
+					new DScroller(EScroll::sc_ceiling, -dx, dy, control, line->frontsector->Index(), accel);
 				}
 			}
 			break;
@@ -498,7 +498,7 @@ void P_SpawnScrollers(void)
 
 					if (line->args[0] == l->args[0] && (line->args[1] & 2))
 					{
-						new DScroller(EScroll::sc_floor, -dx, dy, control, int(line->frontsector - &level.sectors[0]), accel);
+						new DScroller(EScroll::sc_floor, -dx, dy, control, line->frontsector->Index(), accel);
 					}
 				}
 			}
@@ -516,7 +516,7 @@ void P_SpawnScrollers(void)
 
 					if (line->args[0] == l->args[0] && (line->args[1] & 4))
 					{
-						new DScroller (EScroll::sc_carry, dx, dy, control, int(line->frontsector-&level.sectors[0]), accel);
+						new DScroller (EScroll::sc_carry, dx, dy, control, line->frontsector->Index(), accel);
 					}
 				}
 			}
diff --git a/src/p_setup.cpp b/src/p_setup.cpp
index 963f149457..e03b7ac231 100644
--- a/src/p_setup.cpp
+++ b/src/p_setup.cpp
@@ -3179,11 +3179,11 @@ static void P_GroupLines (bool buildmap)
 		auto li = &lines[i];
 		if (li->frontsector != NULL)
 		{
-			li->frontsector->Lines[linesDoneInEachSector[li->frontsector - &level.sectors[0]]++] = li;
+			li->frontsector->Lines[linesDoneInEachSector[li->frontsector->Index()]++] = li;
 		}
 		if (li->backsector != NULL && li->backsector != li->frontsector)
 		{
-			li->backsector->Lines[linesDoneInEachSector[li->backsector - &level.sectors[0]]++] = li;
+			li->backsector->Lines[linesDoneInEachSector[li->backsector->Index()]++] = li;
 		}
 	}
 	
diff --git a/src/p_sight.cpp b/src/p_sight.cpp
index bb3dbdd686..eb62e505a3 100644
--- a/src/p_sight.cpp
+++ b/src/p_sight.cpp
@@ -821,7 +821,7 @@ bool P_CheckSight (AActor *t1, AActor *t2, int flags)
 
 	const sector_t *s1 = t1->Sector;
 	const sector_t *s2 = t2->Sector;
-	int pnum = int(s1 - &level.sectors[0]) * level.sectors.Size() + int(s2 - &level.sectors[0]);
+	int pnum = int(s1->Index()) * level.sectors.Size() + int(s2->Index());
 
 //
 // check for trivial rejection
diff --git a/src/p_spec.cpp b/src/p_spec.cpp
index 0f098f36b4..b3cf9c597b 100644
--- a/src/p_spec.cpp
+++ b/src/p_spec.cpp
@@ -433,7 +433,7 @@ void P_PlayerInSpecialSector (player_t *player, sector_t * sector)
 	if (sector->isSecret())
 	{
 		sector->ClearSecret();
-		P_GiveSecret(player->mo, true, true, int(sector - &level.sectors[0]));
+		P_GiveSecret(player->mo, true, true, sector->Index());
 	}
 }
 
@@ -1157,7 +1157,7 @@ void P_InitSectorSpecial(sector_t *sector, int special)
 
 	case dScroll_EastLavaDamage:
 		P_SetupSectorDamage(sector, 5, 32, 256, NAME_Fire, SECF_DMGTERRAINFX);
-		P_CreateScroller(EScroll::sc_floor, -4., 0, -1, int(sector - &level.sectors[0]), 0);
+		P_CreateScroller(EScroll::sc_floor, -4., 0, -1, sector->Index(), 0);
 		keepspecial = true;
 		break;
 
@@ -1215,14 +1215,14 @@ void P_InitSectorSpecial(sector_t *sector, int special)
 			int i = sector->special - Scroll_North_Slow;
 			double dx = hexenScrollies[i][0] / 2.;
 			double dy = hexenScrollies[i][1] / 2.;
-			P_CreateScroller(EScroll::sc_floor, dx, dy, -1, int(sector-&level.sectors[0]), 0);
+			P_CreateScroller(EScroll::sc_floor, dx, dy, -1, sector->Index(), 0);
 		}
 		else if (sector->special >= Carry_East5 &&
 					sector->special <= Carry_East35)
 		{ // Heretic scroll special
 			// Only east scrollers also scroll the texture
 			P_CreateScroller(EScroll::sc_floor,
-				-0.5 * (1 << ((sector->special & 0xff) - Carry_East5)),	0, -1, int(sector-&level.sectors[0]), 0);
+				-0.5 * (1 << ((sector->special & 0xff) - Carry_East5)),	0, -1, sector->Index(), 0);
 		}
 		keepspecial = true;
 		break;
diff --git a/src/p_tags.cpp b/src/p_tags.cpp
index cb6a405e84..dc71e2663e 100644
--- a/src/p_tags.cpp
+++ b/src/p_tags.cpp
@@ -45,11 +45,6 @@ FTagManager tagManager;
 //
 //-----------------------------------------------------------------------------
 
-static inline int sectindex(const sector_t *sector)
-{
-	return (int)(intptr_t)(sector - &level.sectors[0]);
-}
-
 static inline int lineindex(const line_t *line)
 {
 	return (int)(intptr_t)(line - lines);
@@ -194,7 +189,7 @@ void FTagManager::HashTags()
 
 bool FTagManager::SectorHasTags(const sector_t *sector) const
 {
-	int i = sectindex(sector);
+	int i = sector->Index();
 	return SectorHasTags(i);
 }
 
@@ -206,7 +201,7 @@ bool FTagManager::SectorHasTags(const sector_t *sector) const
 
 int FTagManager::GetFirstSectorTag(const sector_t *sect) const
 {
-	int i = sectindex(sect);
+	int i = sect->Index();
 	return SectorHasTags(i) ? allTags[startForSector[i]].tag : 0;
 }
 
@@ -238,7 +233,7 @@ bool FTagManager::SectorHasTag(int i, int tag) const
 
 bool FTagManager::SectorHasTag(const sector_t *sector, int tag) const
 {
-	return SectorHasTag(sectindex(sector), tag);
+	return SectorHasTag(sector->Index(), tag);
 }
 
 //-----------------------------------------------------------------------------
diff --git a/src/p_tags.h b/src/p_tags.h
index 9649ace1ab..17719de6fb 100644
--- a/src/p_tags.h
+++ b/src/p_tags.h
@@ -90,7 +90,7 @@ public:
 		if (tag == 0)
 		{
 			searchtag = INT_MIN;
-			start = (line == NULL || line->backsector == NULL)? -1 : (int)(line->backsector - &level.sectors[0]);
+			start = (line == NULL || line->backsector == NULL)? -1 : line->backsector->Index();
 		}
 		else
 		{
diff --git a/src/r_defs.h b/src/r_defs.h
index 4b820a3a0a..70816838c3 100644
--- a/src/r_defs.h
+++ b/src/r_defs.h
@@ -23,7 +23,6 @@
 #ifndef __R_DEFS_H__
 #define __R_DEFS_H__
 
-#include <forward_list>
 #include "doomdef.h"
 #include "templates.h"
 #include "memarena.h"
@@ -660,6 +659,7 @@ public:
 	double FindLowestCeilingPoint(vertex_t **v) const;
 	double FindHighestFloorPoint(vertex_t **v) const;
 	void RemoveForceField();
+	int Index() const { return int(this - &level.sectors[0]); }
 
 	void AdjustFloorClip () const;
 	void SetColor(int r, int g, int b, int desat);