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

Offline QwazyWabbit

  • Carpal Tunnel Member
  • ******
  • Posts: 1375
    • 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:

 

Yotematoi

February 14, 2025, 12:41:48 PM
 :-*
 

|iR|Focalor

February 13, 2025, 07:31:24 AM
I was on DM this morning for the first time in a pretty long time. Seemed fine to me.

0rbisson

February 13, 2025, 04:54:21 AM
DM server is fucked. 2 point blank rockets from spawn and lava damage and player survived? WTF?" Also someone has fucked with the spawn points, every single time no matter how big the map you spawn right in front of the person who killed you last
 

rikwad

February 08, 2025, 10:48:18 PM
Seattle FFA now has working HTTP downloads. Thanks Unholy!
 
RIP Pepp   ✟
 
 

-Unh0ly-

February 03, 2025, 01:20:14 AM
vvvALL WEAPS HAVE MUZZLE FLASH vvvv
 

|iR|Focalor

December 25, 2024, 12:15:35 AM
 

|iR|Focalor

December 25, 2024, 12:06:54 AM
 

RailWolf

December 23, 2024, 09:15:50 AM
Fixed the image for you =)
And Die Hard is a great Christmas movie
 

|iR|Focalor

December 19, 2024, 04:55:07 AM
correction - you gotta put the whole word, not just w: {img width=210}

Show 50 latest
Welcome, Guest. Please login or register.
April 02, 2025, 01:14:07 AM

Login with username, password and session length