Module dpmod: Change committed

havoc at icculus.org havoc at icculus.org
Thu Feb 20 04:01:11 EST 2003


Commiter   : havoc
CVSROOT    : /cvs/cvsroot/twilight
Module     : dpmod
Commit time: 2003-02-20 09:01:11 UTC

Log message:

optimized decor counting (now keeps count in numdecors instead of checking each frame, sped up helm18 a bit), limited number of iterations on decor removal in case it might cause a runaway loop

Modified files:
     qc/casings.qc qc/gore.qc qc/decors.qc qc/m_zombie.qc

------=MIME.26573479120a49741f02a00a4c328024
Content-Type: text/plain; name="dpmod.20030220.090111.havoc.diff"
Content-Disposition: attachment; filename="dpmod.20030220.090111.havoc.diff"
Content-Transfer-Encoding: 8bit

Index: dpmod/qc/casings.qc
diff -u dpmod/qc/casings.qc:1.2 dpmod/qc/casings.qc:1.3
--- dpmod/qc/casings.qc:1.2	Thu Feb 13 21:47:16 2003
+++ dpmod/qc/casings.qc	Thu Feb 20 04:01:01 2003
@@ -36,7 +36,7 @@
 	p = pointcontents(self.origin);
 	if (p == CONTENT_SOLID || p == CONTENT_LAVA || p == CONTENT_SKY)
 	{
-		remove(self);
+		removedecor(self);
 		return;
 	}
 	if (time > self.cnt)
@@ -44,7 +44,7 @@
 		self.nextthink = time;
 		self.alpha = self.alpha - frametime;
 		if (self.alpha < 0.0625)
-			remove(self);
+			removedecor(self);
 	}
 };
 
Index: dpmod/qc/decors.qc
diff -u dpmod/qc/decors.qc:1.1.1.1 dpmod/qc/decors.qc:1.2
--- dpmod/qc/decors.qc:1.1.1.1	Thu Sep 19 15:06:56 2002
+++ dpmod/qc/decors.qc	Thu Feb 20 04:01:01 2003
@@ -1,7 +1,8 @@
 
-float   maxclients; // set by worldspawn code
-float   maxdecors;
-.float  createdtime;
+float maxclients; // set by worldspawn code
+float numdecors;
+float maxdecors;
+.float createdtime;
 .void() th_gib;
 
 //void(vector org, entity en, vector dir, float splattype, float importance) newbloodsplat;
@@ -11,29 +12,39 @@
 void() decorframe =
 {
 	local entity estart, e, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
-	local float bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, c;
-	// first count up all the decors
-	// (QC is slow, so doing the find() twice is faster than merging
-	//  the two loops, as long as the second loop is rarely used)
-	// later note - created findchain, much faster now
-	c = 0;
+	local float bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt10, iterations;
+
+	// numdecors is allowed to be bogus as long as it is >= the real number of decors
+	// (but perfection is clearly preferable)
+	if (numdecors <= maxdecors)
+		return;
+
+	// recount all the decors
+	numdecors = 0;
 	estart = e = findchain(classname, "decor");
 	while(e)
 	{
-		c = c + 1;
+		numdecors = numdecors + 1;
 		e = e.chain;
 	}
-	if (c <= maxdecors)
+	if (numdecors <= maxdecors)
 		return; // nothing to do
 
-	while (c > maxdecors)
+	// limit it to considering 1000 entities per frame,
+	// otherwise it can cause a runaway loop error
+	iterations = 0;
+	while (numdecors > maxdecors && iterations < 1000)
 	{
-		// find and remove the oldest decors (upto 10)
+		iterations = iterations + 1;
+		// find and remove the oldest decors (upto 10 at once)
 		b1 = b2  = b3  = b4  = b5  = b6 = b7 = b8 = b9 = b10 = world;
 		bt1 = bt2 = bt3 = bt4 = bt5 = bt6 = bt7 = bt8 = bt9 = bt10 = time + 10000;
+		if (iterations > 0)
+			estart = findchain(classname, "decor");
 		e = estart;
 		while(e)
 		{
+			iterations = iterations + 1;
 			if (e.createdtime < bt10)
 			{
 				if (e.createdtime < bt9)
@@ -150,27 +161,34 @@
 			e = e.chain;
 		}
 		// remove the oldest decors
-		                    c = c - 1;remove(b1);
-		if (c > maxdecors) {c = c - 1;remove(b2);}
-		if (c > maxdecors) {c = c - 1;remove(b3);}
-		if (c > maxdecors) {c = c - 1;remove(b4);}
-		if (c > maxdecors) {c = c - 1;remove(b5);}
-		if (c > maxdecors) {c = c - 1;remove(b6);}
-		if (c > maxdecors) {c = c - 1;remove(b7);}
-		if (c > maxdecors) {c = c - 1;remove(b8);}
-		if (c > maxdecors) {c = c - 1;remove(b9);}
-		if (c > maxdecors) {c = c - 1;remove(b10);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b1);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b2);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b3);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b4);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b5);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b6);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b7);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b8);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b9);}
+		if (numdecors > maxdecors) {numdecors = numdecors - 1;remove(b10);}
 	}
 };
 
 entity() newdecor =
 {
-	local   entity  e;
+	local entity e;
+	numdecors++;
 	e = spawn();
 	e.classname = "decor";
 	return e;
 };
 
+void(entity e) removedecor =
+{
+	numdecors--;
+	remove(e);
+};
+
 /*
 void()  s_animthink =
 {
@@ -280,7 +298,7 @@
 	self.nextthink = time;
 	self.flags = self.flags - (self.flags & FL_ONGROUND);
 	if (pointcontents(self.origin) == CONTENT_SOLID)
-		remove(self);
+		removedecor(self);
 };
 
 .float  createdtime;
Index: dpmod/qc/gore.qc
diff -u dpmod/qc/gore.qc:1.3 dpmod/qc/gore.qc:1.4
--- dpmod/qc/gore.qc:1.3	Sat Sep 21 06:10:16 2002
+++ dpmod/qc/gore.qc	Thu Feb 20 04:01:01 2003
@@ -28,7 +28,7 @@
 	if (p == CONTENT_SOLID || p == CONTENT_SKY || p == CONTENT_LAVA)
 	{
 		if (self.classname == "decor")
-			remove(self);
+			removedecor(self);
 		else
 			self.model = "";
 		return;
@@ -42,7 +42,7 @@
 		{
 			self.nextthink = 0;
 			if (self.classname == "decor")
-				remove(self);
+				removedecor(self);
 			else
 				self.model = "";
 		}
@@ -298,11 +298,14 @@
 };
 
 // used specifically by monsters, choose any of the above methods
-// here to affect all monsters.
+// here to affect all monsters except zombies.
 void(entity t, void() framefunc) MonsterCorpse =
 {
-	t.classname = "decor"; // setup for recycling
-	t.createdtime = time + 30; // make sure it won't be replaced anytime soon
+	// setup for recycling
+	t.classname = "decor";
+	numdecors++;
+	// make sure it won't be replaced anytime soon
+	t.createdtime = time + 30;
 	BecomeCorpse(t, framefunc);
 };
 
Index: dpmod/qc/m_zombie.qc
diff -u dpmod/qc/m_zombie.qc:1.3 dpmod/qc/m_zombie.qc:1.4
--- dpmod/qc/m_zombie.qc:1.3	Thu Feb 13 21:41:43 2003
+++ dpmod/qc/m_zombie.qc	Thu Feb 20 04:01:01 2003
@@ -451,7 +451,10 @@
 
 void() zombie_die =
 {
-	self.classname = "decor"; // setup for recycling
+	// setup for recycling
+	self.classname = "decor";
+	numdecors++;
+
 	self.createdtime = time + 30; // make sure it won't be replaced anytime soon
 	sound (self, CHAN_VOICE, "zombie/z_gib.wav", 1, ATTN_NORM);
 	MonsterGibs("progs/h_zombie.mdl", 4, "", 0, "", 0);


More information about the twilight-commits mailing list