.qc (dot qc) - the group for quake c coders of all denominations. If you make quake one mods and write code in quake c, join our group! I'll try to answer any quake c questions - please post your inquiry in the forums...

Forum Thread
  Posts  
qc++ avoid pointless code (Groups : qc : Forum : vault() : qc++ avoid pointless code) Locked
Thread Options
numbersix
numbersix quake-c coder++
Dec 22 2014 Anchor

So I'm finally recoding v106 quake-c.

Notes about avoiding pointless code:

void() make_bubbles =
{
local entity bubble;

bubble = spawn();
// bubble.solid = SOLID_NOT;
// bubble.frame = 0;
// bubble.cnt = 0;

I commented the last 3 statements out - why?
They are pointless.

When you get an entity pointer back from spawn() -
for all entity field variable: floats = 0, vector = '0 0 0', string = "", entity pointers = world and function pointers are (void) 0.

You dont need to make any of these assignments immediately after calling spawn().

Entity field variables are those assigned:
.float {fieldname} ;
.vector {fieldname} ;
.string {fieldname} ;
.entity {fieldname} ;
.void() {fieldname} ;

SOLID_NOT = 0, btw.

This is also redundant:

void() somefn =
{
// vars code

//return; // no point to this - it sucks up quake-c resources and does nothing
};


Pointless code 2:

other = find (world, classname, "player");
while (other != world)
{
/* vars code */
other = find (other, classname, "player");
}

"other != world" is pointless - you can simply use:

while (other) // != world)

For any entity test condition, the world is entity 0.
So a non-zero result is NOT world. A zero result is world.

Thus (other) is always true when other is not world.
The same contrapositive test applies - (! other) is always true when other is world.

This results in the same logic and saves statements and space.


Logic test results for 0, '0 0 0', "", world and function zero case comparison:

--- logic tests - float: 0 if (f) false if (! f) true if (f != 0) false if (f == 0) true
--- logic tests - float: 1 if (f) true if (! f) false if (f != 0) true if (f == 0) false
--- logic tests - float: 2 if (f) true if (! f) false if (f != 0) true if (f == 0) false
--- logic tests - vector: ' 0.0 0.0 0.0' if (v) false if (! v) true if (v != '0 0 0') false if (v == '0 0 0') true
--- logic tests - vector: ' 7.0 0.0 7.0' if (v) true if (! v) false if (v != '0 0 0') true if (v == '0 0 0') false
--- logic tests - string: "" if (s) false if (! s) true if (s != "") false if (s == "") true
--- logic tests - string: "str" if (s) true if (! s) false if (s != "") true if (s == "") false
--- logic tests - entity: entity 0 if (e) false if (! e) true if (e != world) false if (e == world) true
--- logic tests - entity: entity 1 if (e) true if (! e) false if (e != world) true if (e == world) false
--- logic tests - entity: entity 1 if (e) true if (! e) false if (e != world) true if (e == world) false
--- logic tests - void: NO VALUE if (fn) false if (! fn) true if (fn != NextLevel) true if (fn == NextLevel) false
--- logic tests - void: NextLevel if (fn) true if (! fn) false if (fn != NextLevel) false if (fn == NextLevel) true
(as always this interface murders spacing - here is a console image: Moddb.com )

This enumerates all logic test 0 cases.

The test clearly demonstrates:
if ({var}) is equivalent to: if ({var} != {zero case})
if (! {var}) is equivalent to: if ({var} == {zero case})

With the exception of having _no_ zero case for function calls. You can only test against existing functions.
(Thus the logic indication on the if(fn) line.)

Note: this logical equivalence only works when the zero test case is specified. Other values, and <=, >= tests will not use this logic.


Followup:

It has come to my attention that the fteqcc optimizer option -O{option,1,2,3} may possibly
negate the need for adapting the zero case logic tests. Also a possibility is that some logic
tests do not add to progs.dat bloat.

Un-needed code can still be left out as it uses resources.

I have also realized that my quest against code bloat is likely best applied to complex code and
large projects. Beginners and small project authors should write code they feel comfortable with.

The best advice I can give: enjoy your code.

Support free code by Six gaming on Patreon

Edited by: numbersix

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.