From 82a6d0b44c00fa31a07b3cfca2a804a58d5d37aa Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@zdoom.fake>
Date: Fri, 21 Mar 2008 12:25:45 +0000
Subject: [PATCH] - Added Karate Chris's ThingCountSector submission.

SVN r830 (trunk)
---
 docs/rh-log.txt |  1 +
 src/p_acs.cpp   | 40 ++++++++++++++++++++++++++++------------
 src/p_acs.h     |  4 +++-
 3 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/docs/rh-log.txt b/docs/rh-log.txt
index ba9e5f889..7ed763d56 100644
--- a/docs/rh-log.txt
+++ b/docs/rh-log.txt
@@ -1,4 +1,5 @@
 March 21, 2008 (Changes by Graf Zahl)
+- Added Karate Chris's ThingCountSector submission.
 - Made texture indices in FSwitchDef full integers. Since that required
   some data restructuring I also eliminated the MAX_FRAMES limit of 128
   per switch.
diff --git a/src/p_acs.cpp b/src/p_acs.cpp
index 0aff6279d..73b543f4a 100644
--- a/src/p_acs.cpp
+++ b/src/p_acs.cpp
@@ -1796,7 +1796,7 @@ int DLevelScript::Random (int min, int max)
 	return min + pr_acs(max - min + 1);
 }
 
-int DLevelScript::ThingCount (int type, int stringid, int tid)
+int DLevelScript::ThingCount (int type, int stringid, int tid, int tag)
 {
 	AActor *actor;
 	const PClass *kind;
@@ -1838,11 +1838,14 @@ do_count:
 			if (actor->health > 0 &&
 				(kind == NULL || actor->IsA (kind)))
 			{
-				// Don't count items in somebody's inventory
-				if (!actor->IsKindOf (RUNTIME_CLASS(AInventory)) ||
-					static_cast<AInventory *>(actor)->Owner == NULL)
+				if (actor->Sector->tag == tag || tag == -1)
 				{
-					count++;
+					// Don't count items in somebody's inventory
+					if (!actor->IsKindOf (RUNTIME_CLASS(AInventory)) ||
+						static_cast<AInventory *>(actor)->Owner == NULL)
+					{
+						count++;
+					}
 				}
 			}
 		}
@@ -1855,11 +1858,14 @@ do_count:
 			if (actor->health > 0 &&
 				(kind == NULL || actor->IsA (kind)))
 			{
-				// Don't count items in somebody's inventory
-				if (!actor->IsKindOf (RUNTIME_CLASS(AInventory)) ||
-					static_cast<AInventory *>(actor)->Owner == NULL)
+				if (actor->Sector->tag == tag || tag == -1)
 				{
-					count++;
+					// Don't count items in somebody's inventory
+					if (!actor->IsKindOf (RUNTIME_CLASS(AInventory)) ||
+						static_cast<AInventory *>(actor)->Owner == NULL)
+					{
+						count++;
+					}
 				}
 			}
 		}
@@ -3572,17 +3578,27 @@ int DLevelScript::RunScript ()
 			break;
 
 		case PCD_THINGCOUNT:
-			STACK(2) = ThingCount (STACK(2), -1, STACK(1));
+			STACK(2) = ThingCount (STACK(2), -1, STACK(1), -1);
 			sp--;
 			break;
 
 		case PCD_THINGCOUNTDIRECT:
-			PushToStack (ThingCount (pc[0], -1, pc[1]));
+			PushToStack (ThingCount (pc[0], -1, pc[1], -1));
 			pc += 2;
 			break;
 
 		case PCD_THINGCOUNTNAME:
-			STACK(2) = ThingCount (-1, STACK(2), STACK(1));
+			STACK(2) = ThingCount (-1, STACK(2), STACK(1), -1);
+			sp--;
+			break;
+
+		case PCD_THINGCOUNTNAMESECTOR:
+			STACK(2) = ThingCount (-1, STACK(3), STACK(2), STACK(1));
+			sp--;
+			break;
+
+		case PCD_THINGCOUNTSECTOR:
+			STACK(2) = ThingCount (STACK(3), -1, STACK(2), STACK(1));
 			sp--;
 			break;
 
diff --git a/src/p_acs.h b/src/p_acs.h
index b94843a2b..c97fa1790 100644
--- a/src/p_acs.h
+++ b/src/p_acs.h
@@ -548,6 +548,8 @@ public:
 		PCD_CHECKACTORFLOORTEXTURE,
 /*340*/	PCD_GETACTORLIGHTLEVEL,
 		PCD_SETMUGSHOTSTATE,
+		PCD_THINGCOUNTSECTOR,
+		PCD_THINGCOUNTNAMESECTOR,
 
 		PCODE_COMMAND_COUNT
 	};
@@ -668,7 +670,7 @@ protected:
 	void PutLast ();
 	void PutFirst ();
 	static int Random (int min, int max);
-	static int ThingCount (int type, int stringid, int tid);
+	static int ThingCount (int type, int stringid, int tid, int tag);
 	static void ChangeFlat (int tag, int name, bool floorOrCeiling);
 	static int CountPlayers ();
 	static void SetLineTexture (int lineid, int side, int position, int name);