Author Topic: Toward a better DoRespawn  (Read 1147 times)

Offline QwazyWabbit

  • Carpal Tunnel Member
  • ******
  • Posts: 1372
    • View Profile
  • Rated:
Toward a better DoRespawn
« on: March 22, 2020, 04:20:47 AM »
Every time I had to look at DoRespawn it always annoyed me to see these lines in it:

Code: [Select]
for (count = 0, ent = master; ent; ent = ent->chain, count++)
;

and

Code: [Select]
for (count = 0, ent = master; count < choice; ent = ent->chain, count++)
;

The crazy comma operators and the dangling semicolon always pressed my WFT button.

Since my age and health conditions make it dangerous for me to venture forth into the dirty world of Coronavirus infestations and since zombie-making infection carriers are indistinguishable from normal humans unless you have infrared vision to spot the fevers, I am confined to my home for the weekend, doing my part to flatten the curve and save the world. I was "essential personnel" Friday and I will be again Monday but once I solve one problem Monday I will be back to saving the world by not participating in it.

So I decided to focus on this one function in Q2 mods called DoRespawn. This is the "Think" function that gets triggered when it's time to respawn items like armor shards, armor jackets, weapons, health boxes and pills, megas, etc. Anything that gets picked up and needs to be respawned after a scheduled time period.

Modern C has different rules than it did back in the days of the creation of Q2 and Microsoft Visual C++ of 1997. Some idioms remain but the particular idiom used by Zoid just makes a code maintainer stop and say "Oh? Hmmmm." Modern C compilers are also better at optimizing code so it's not necessary to resort to idioms in an attempt to make the code faster. Succinct C is not necessarily efficient C.

So here's a fix for the most basic version of DoRespawn with the idiomatic syntax removed and some error checking and reporting in place and some coverage for cases of null pointer dereference. Some versions have more code in place to take various actions on respawn but my primary goal was to refactor the for loops into something more easily understood at a glance and to maintain.


Code: [Select]
void DoRespawn(edict_t* ent)
{
if (ent == NULL)
{
gi.dprintf("NULL ent passed to %s\n", __func__);
return;
}

if (ent->team)
{
edict_t* master;
unsigned count;
unsigned choice;

master = ent->teammaster;
if (master == NULL)
return;

count = 0;
for (ent = master; ent; ent = ent->chain)
count++;

assert(count != 0);
choice = rand() % count;

count = 0;
for (ent = master; count < choice; ent = ent->chain)
count++;
}

if (ent)
{
ent->svflags &= ~SVF_NOCLIENT;
ent->solid = SOLID_TRIGGER;
gi.linkentity(ent);

// send an effect
ent->s.event = EV_ITEM_RESPAWN;
}
}

QW
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus

 

El Box de Shoutamente

Last 10 Shouts:

Costigan_Q2

November 11, 2024, 06:41:06 AM
"Stay cozy folks.

Everything is gonna be fine."

There'll be no excuses for having TDS after January 20th, there'll be no excuses AT ALL!!!
 

|iR|Focalor

November 06, 2024, 03:28:50 AM
 

RailWolf

November 05, 2024, 03:13:44 PM
Nice :)

Tom Servo

November 04, 2024, 05:05:24 PM
The Joe Rogan Experience episode 223 that dropped a couple hours ago with Musk, they're talking about Quake lol.

Costigan_Q2

November 04, 2024, 03:37:55 PM
Stay cozy folks.

Everything is gonna be fine.
 

|iR|Focalor

October 31, 2024, 08:56:37 PM

Costigan_Q2

October 17, 2024, 06:31:53 PM
Not activated your account yet?

Activate it now! join in the fun!

Tom Servo

October 11, 2024, 03:35:36 PM
HAHAHAHAHAHA
 

|iR|Focalor

October 10, 2024, 12:19:41 PM
I don't worship the devil. Jesus is Lord, friend. He died for your sins. He will forgive you if you just ask.
 

rikwad

October 09, 2024, 07:57:21 PM
Sorry, I couldn't resist my inner asshole.

Show 50 latest
Welcome, Guest. Please login or register.
November 21, 2024, 04:56:57 PM

Login with username, password and session length