Forum

Random classes

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Random classes

Postby MeTcHsteekle » Fri Sep 04, 2009 1:20 am

so I'm having an issue [big surprise there eh?] with having random classes for players when they enter the game, it seems to just run through them really really fast instead of just having one until you die [then you get a hopefully different class] this is what i got so far:

Code: Select all
void() getclass =
{
   local float   c;
   
   c = rint ((random()*3) + 1);
   
   if (c == 1)
   {
      //self.classtype = 0;
      centerprint(self, "light");
      self.effects = self.effects + EF_BRIGHTLIGHT;
   }
   if (c == 2)
   {
      //self.classtype = 1;
      centerprint(self, "health");
   }
   if (c == 3)
   {
      //self.classtype = 2;
      centerprint(self, "armor");
   }
   if (c == 4)
   {
      //self.classtype = 3;
      centerprint(self, "speed");
   }
};   

im not sure what the problem is :/?
bah
MeTcHsteekle
 
Posts: 399
Joined: Thu May 15, 2008 10:46 pm
Location: its a secret

Postby Electro » Fri Sep 04, 2009 12:49 pm

For starters:

Code: Select all
void() getclass =
{
   local float   c;
   
   c = ceil(random() * 3);
   
   if (c == 1)
   {
      //self.classtype = 0;
      centerprint(self, "light");
      self.effects = self.effects + EF_BRIGHTLIGHT;
   }
   else if (c == 2)
   {
      //self.classtype = 1;
      centerprint(self, "health");
   }
   else if (c == 3)
   {
      //self.classtype = 2;
      centerprint(self, "armor");
   }
   else if (c == 4)
   {
      //self.classtype = 3;
      centerprint(self, "speed");
   }
};


Secondly... where are you calling it?
Benjamin Darling
http://www.bendarling.net/

Reflex - In development competitive arena fps combining modern tech with the speed, precision and freedom of 90's shooters.
http://www.reflexfps.net/
Electro
 
Posts: 312
Joined: Wed Dec 29, 2004 11:25 pm
Location: Brisbane, Australia

Postby MeTcHsteekle » Fri Sep 04, 2009 6:15 pm

i got a call for it in playerprethink in client.qc

under
Code: Select all
makevectors (self.v_angle);      // is this still used

   CheckRules ();
   WaterMove ();


and that code i posted first is in weapons.qc underneath
Code: Select all
// called by worldspawn
void() W_Precache =
{
   precache_sound ("weapons/r_exp3.wav");   // new rocket explosion
   precache_sound ("weapons/rocket1i.wav");   // spike gun
   precache_sound ("weapons/sgun1.wav");
   precache_sound ("weapons/guncock.wav");   // player shotgun
   precache_sound ("weapons/ric1.wav");   // ricochet (used in c code)
   precache_sound ("weapons/ric2.wav");   // ricochet (used in c code)
   precache_sound ("weapons/ric3.wav");   // ricochet (used in c code)
   precache_sound ("weapons/spike2.wav");   // super spikes
   precache_sound ("weapons/tink1.wav");   // spikes tink (used in c code)
   precache_sound ("weapons/grenade.wav");   // grenade launcher
   precache_sound ("weapons/bounce.wav");      // grenade bounce
   precache_sound ("weapons/shotgn2.wav");   // super shotgun
};

float() crandom =
{
   return 2*(random() - 0.5);
};


...whats the diffrence between rint and ceil random :O?
bah
MeTcHsteekle
 
Posts: 399
Joined: Thu May 15, 2008 10:46 pm
Location: its a secret

Postby Dr. Shadowborg » Fri Sep 04, 2009 10:17 pm

Well, first playerprethink is a horrible place to put it. Put it somewhere where it will work properly, like somewhere in putclientinserver() or some such.

Also, crandom isn't short for ceil random. (I have no idea what it's short for actually) What it does is flip at random, from either a positive or a negative value. i.e. crandom()*1 will result in either regular 1 or -1. (Yes, I know this explanation sucks.)
User avatar
Dr. Shadowborg
InsideQC Staff
 
Posts: 1110
Joined: Sat Oct 16, 2004 3:34 pm

Postby MeTcHsteekle » Sat Sep 05, 2009 2:13 am

ah okay, i had a feeling that prethink wasn't gonna work for some reason, but i didn't know where i should have put it.

also, that explanation is better then the one in the qcrm which was:
10.1.2. ceil
float ceil(float val)
Returns val, rounded up to the integer above (like the equivalent function in C).
.

anyways thanks guys it works now :D, now to have the classes actually do something...
bah
MeTcHsteekle
 
Posts: 399
Joined: Thu May 15, 2008 10:46 pm
Location: its a secret

Postby Dr. Shadowborg » Sat Sep 05, 2009 3:43 am

Extra tip:

rint will return whatever you feed it rounded to whatever integer is closest. i.e. 0.1 will return 0 whereas 0.9 will return 1.

ceil will always round up whatever you feed it, meaning regardless of whether it's 0.1 or 0.9 it will always return 1.

floor does the exact opposite of ceil. (Always rounding down, meaning regardless of whether you feed it 0.1 or 0.9 it will always return 0.)

These are actually pretty useful functions when you don't want fractional figures and whatnot. (i.e. wierd stuff like "you got 1.5 shells" :wink: )
User avatar
Dr. Shadowborg
InsideQC Staff
 
Posts: 1110
Joined: Sat Oct 16, 2004 3:34 pm

Re: Random classes

Postby FrikaC » Tue Sep 08, 2009 2:09 pm

MeTcHsteekle wrote: self.effects = self.effects + EF_BRIGHTLIGHT;


Use the | symbol, not +. Plus means to add the value of EF_BRIGHTFIELD (1) in. If it's already got this bit set it will clear it, and flip the next highest bit and so on up, ie addition. Meaning if its 1, it will become 2 which is EF_MUZZLEFLASH. If 2, it will become 3, if 3 it will become 4. As it ever increases all the bits, since we're adding 1, will be toggled. Each of those values are combinations of completely unrelated bit flags.

The | symbol "if bit is set on the left side OR on the right side, the bit is set in the result". This is the typical operation used in setting bitflags.
FrikaC
Site Admin
 
Posts: 1026
Joined: Fri Oct 08, 2004 11:19 pm

Postby MeTcHsteekle » Wed Sep 09, 2009 12:54 am

ah thanks for the info guys, greatly appreciated :}
bah
MeTcHsteekle
 
Posts: 399
Joined: Thu May 15, 2008 10:46 pm
Location: its a secret

Postby Lardarse » Tue Sep 15, 2009 1:40 am

Dr. Shadowborg wrote:Also, crandom isn't short for ceil random. (I have no idea what it's short for actually)

Centered random?
Roaming status: Testing and documentation
User avatar
Lardarse
 
Posts: 266
Joined: Sat Nov 05, 2005 1:58 pm
Location: Bristol, UK

Postby Orion » Tue Sep 15, 2009 3:35 am

The difference between random() and crandom() is that random() returns a decimal between 0 and 1. crandom() will return a decimal between -1 and 1. :)
User avatar
Orion
 
Posts: 476
Joined: Fri Jan 12, 2007 6:32 pm
Location: Brazil


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest