Forum

choose a random number

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

choose a random number

Postby domis4 » Mon Jan 02, 2012 4:20 pm

Hi everyone,

im actually modding my quake into a turn based battle system.

the basically source of the turn- based battle i do have allready scripted (less or more...)

but its pretty stupid to use an impulse command or put objects into the map in order to start a battle.
so i decidet to ask if the player walks and the current map is a battle map. (for random battles)

the theory is: if the battlevariable is true and the player walks, a 90:10 chance is there to start a battle.
so, how do i do it? (the chance)

my second question is where is the part for walking. So that i can ask if walking is allowed or not.
things ive tried:
Code: Select all
entity first;
for (self.velocity_x = 0;
   self.velocity_y = 0;
         centerprint(self, "inbattle is finally 1!")) //this is just a answer for me, that it worked^^
return first;   




greetings
User avatar
domis4
 
Posts: 19
Joined: Mon Dec 26, 2011 7:29 pm

Re: choose a random number

Postby r00k » Mon Jan 02, 2012 9:31 pm

QuakeC has a built-in random function.

you can try this one
Code: Select all
// 2001-11-18 Random number function  start
/*
General solution to always get a valid random number of a given range and stepsize.

All possible values have the same chance of being calculated.
(Ok, the last value has a slighlty higher chance :)

It works for negative and positive parameters, only the stepsize should fit the range:
end > start : positive stepsize
start > end : negative stepsize

Note that the typical "result = START + floor( random() * ( COUNT - 0.01 ) )"
only works for positive integer ranges with stepsize +1.
*/
float(float start, float end, float stepsize) rnd_number =
{
   local float   rcnt;
   local float   rnd;
   local float result;

   // calculate the number of possible different values
   rcnt = floor( fabs( ( end - start ) / stepsize ) ) + 1;

   // calculate the random part of the result value
   rnd = floor( random() * rcnt );
   if ( rnd == rcnt )
   {
      rnd = rnd - 1;
   }

   // calculate the result value
   result = start + ( rnd * stepsize );

   return result;
};
// 2001-11-18 Random number function  end


You can determine if someone is walking based on the animation frame, as turning/jumping will carry a velocity...
r00k
 
Posts: 1110
Joined: Sat Nov 13, 2004 10:39 pm

Re: choose a random number

Postby domis4 » Tue Jan 03, 2012 10:06 am

okay, first thank alot it really helped me.


so with randomnr(); i use the script. every step it calculates a number and prints it to me in the console via sprint(self, result);

but, i always get a red "0" as result. Maybe there is anything i have to attent. The only modified part is, where i put the sprint.
Code: Select all
   void() randomnr =
{
{float(float start, float end, float stepsize) number =
{
   local float   rcnt;
   local float   rnd;
   local float result;
   

   // calculate the number of possible different values
   rcnt = floor( fabs( ( end - start ) / stepsize ) ) + 1;

   // calculate the random part of the result value
   rnd = floor( random() * rcnt );
   if ( rnd == rcnt )
   {
      rnd = rnd - 1;
   }

   // calculate the result value
   result = start + ( rnd * stepsize );
      sprint(self, result); //<---------right here i put it
   sprint(self, "\n");
   return result;

 
 
 
}}

};
 


another question:
i only found the part of walking animations, so i only can determine the animation of walking. Is the part for walking in the player.qc?
User avatar
domis4
 
Posts: 19
Joined: Mon Dec 26, 2011 7:29 pm

Re: choose a random number

Postby r00k » Wed Jan 04, 2012 10:04 am

the result is a float that needs to be converted to a string with ftos

s = ftos(result);
sprint(self,s);
sprint(self,"\n");
ya the walk frams are in player.qc
;)
r00k
 
Posts: 1110
Joined: Sat Nov 13, 2004 10:39 pm

Re: choose a random number

Postby domis4 » Wed Jan 04, 2012 10:34 am

thanks again :D

ive compiled everything, errorless, but its still prints a "0".

of course i defined the "s":

local string s;
User avatar
domis4
 
Posts: 19
Joined: Mon Dec 26, 2011 7:29 pm

Re: choose a random number

Postby Spike » Wed Jan 04, 2012 5:43 pm

local string s;
s = ftos(random() * 64);
sprint(self, "the magic random number is ");
sprint(self, s);
sprint(self, "\n");

will give a number between 0 and 64.
random() returns a number between 0 and 1, so floor(random()) will (nearly) always return 0.

don't define functions inside functions, that's unsupported.

if you're using fteqcc, random(4, 64) will return a number between 4 and 64.
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Re: choose a random number

Postby domis4 » Wed Jan 04, 2012 5:46 pm

thanks dude :)

EDIT:
well, it aint over -.-

Code: Select all
void () randomnr =
{

self.number_random = ftos(random() * 20);
sprint(self, self.number_random);
sprint(self, "\n");

if (self.number_random >= 10 && self.number_random <= 11) {centerprint(self, "yeah, finally it worked!\n");}

}


but it didnt print the message, if its between 10 and 11 ._.
User avatar
domis4
 
Posts: 19
Joined: Mon Dec 26, 2011 7:29 pm

Re: choose a random number

Postby frag.machine » Thu Jan 05, 2012 1:12 am

Code: Select all
void () randomnr =
{

self.number_random = ftos(random() * 20);
sprint(self, self.number_random);
sprint(self, "\n");

if (self.number_random >= 10 && self.number_random <= 11) {centerprint(self, "yeah, finally it worked!\n");}


If self.number_random is a string (I am assuming this because the first line in your function), I suspect your comparison will always fail. AFAIK standard QuakeC can't do tests like:
Code: Select all
if ("10" > "11")


I'd suggest you to always a)work with random numbers using numeric values, and b) round them with integers for comparisons. Remember, 1.01 does not compares equals to 1. An example:
Code: Select all
void () random_func =
{
  local float r;

  r = rint (random () * 5);

  bprint ("random () got me a ");
  bprint (ftos (r));
  if ((r == 0) || (r == 2) || (r == 4))
  {
    bprint (", an even number ");
  }
  else
  {
    bprint (", an odd number ");
  }

  if (r < 3)
  {
    bprint ("lesser than 3.\n");
  }
  else
  {
    bprint ("equals or greater than 3.\n");
  }
};

I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
User avatar
frag.machine
 
Posts: 2090
Joined: Sat Nov 25, 2006 1:49 pm


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest