Author Topic: How do I randomize things?  (Read 3112 times)

0 Members and 1 Guest are viewing this topic.

Offline utkesmer

  • Major(1)
  • Posts: 44
How do I randomize things?
« on: June 10, 2011, 02:37:52 pm »
I often try to learn new things by reading other scripts related to my issue. Nearly all scripts in forums are uploaded to soldatcentral.com, but as you all know it is gone now, so I can't find many to study.

I want to learn how can I randomize things. How I can select a random map? How I can select a random ID? How I can set a random password? I want to learn stuff like these. I hope you will help me.

I've tried to do something about selecting a random ID by using this topic.

Code: [Select]
function RandomID: integer;
var
a: array of integer;
x, y, z: integer;
begin
 for x := 1 to 32 do begin
      if GetPlayerStat(x, 'Active') then begin
           a[y] := x;
           y := y+1;
           SetArrayLength(a, y);
      end;
 end;
 z := NumPlayers;
 case Random(1, z+1) of
      1: Result := a[0]
      2: Result := a[1]
      3: Result := a[2]
      4: Result := a[3]
      5: Result := a[4]
      6: Result := a[5]
      .
      .   // Tried to do something like that but I don't know what to do here. :)
      .
      z: Result := a[z-1]
 end;
end;

Sorry for my grammar mistakes if there is any.

Thanks, Utku.
« Last Edit: June 10, 2011, 05:14:59 pm by utkesmer »

DarkCrusade

  • Guest
Re: How do I randomize things?
« Reply #1 on: June 10, 2011, 02:46:22 pm »
For random maps:
1) Load the maplist into a dynamic array
2) Create a random value between 1 and the length of the array
3) Load that map

For random ID:
1) Idle through the list of players (1 to 32). If an ID is active, put the ID into a dynamic array.
2) Same step as above
3) Success

1) Create an array containing all letters ("a" to "z" and "0" to "9")
2) Create random numbers, until the length of your choice is reached
3) Success

You do the coding :P

Offline utkesmer

  • Major(1)
  • Posts: 44
Re: How do I randomize things?
« Reply #2 on: June 10, 2011, 02:50:03 pm »
Did you read my script above for random ID? I tried to do same but I couldn't.

Offline iDante

  • Veteran
  • *****
  • Posts: 1967

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped

Offline jrgp

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 5037
Re: How do I randomize things?
« Reply #5 on: June 10, 2011, 04:37:14 pm »
http://forums.soldat.pl/index.php?topic=31798.0
soldatcentral is no more

That's pretty douchy of enesce to just have soldatcentral redirect to his commercial software ad site without even giving a notice.
There are other worlds than these

Offline utkesmer

  • Major(1)
  • Posts: 44
Re: How do I randomize things?
« Reply #6 on: June 10, 2011, 04:44:31 pm »
Thank you iDante, I am pretty sure it works perfectly but I couldn't understand how it exactly works.

And I made new one, seems it's going to work.

Code: [Select]
function RandomID: integer;
var
a: array of integer;
x, y: integer;
begin
 for x := 1 to 32 do begin
      if GetPlayerStat(x, 'Active') then begin
           a[y] := x;
           y := y+1;
           SetArrayLength(a, y);
      end;
 end;
 Result := a[Random(0, y)];
end;

It didn't, can you tell me what is wrong?
« Last Edit: June 10, 2011, 05:15:48 pm by utkesmer »

DarkCrusade

  • Guest
Re: How do I randomize things?
« Reply #7 on: June 10, 2011, 05:36:41 pm »
You try to save an integer into an array slot which is non-existant.

Just move "y:=y+1; SetArrayLength" before "a[y]:=x;"

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: How do I randomize things?
« Reply #8 on: June 10, 2011, 05:43:37 pm »
@utkesmer:
Dynamic arrays start empty (in most cases). This means a has length 0. When the first time the code tries to do a[y] := whatever, it is trying to index the 0th index of a, which is not yet defined. You must set the length of the array (a) first, and then modify the value at an index in a.

Sometimes, when a function returns a variant value that is a boolean (such as GetPlayerStat), you cannot simply do if (...) then; you must do if (... = true) then. This could cause an issue (which I've experienced before). In your case, simply replace if GetPlayerStat(x, 'Active') then with if GetPlayerStat(x, 'Active') = true then.

Code: [Select]
function RandomID: integer;
var
a: array of integer;
x, y: integer;
begin
 for x := 1 to 32 do begin
      if GetPlayerStat(x, 'Active') = true then begin
           SetArrayLength(a, y + 1);
           a[y] := x;
           y := y+1;
      end;
 end;
 Result := a[Random(0, y)];
end;
Notice how the order changed, and y changed to y+1 for setting the array length of a.
Try this (it is untested).
« Last Edit: June 10, 2011, 05:45:08 pm by DorkeyDear »

Offline utkesmer

  • Major(1)
  • Posts: 44
Re: How do I randomize things?
« Reply #9 on: June 10, 2011, 06:16:48 pm »
I got it and tested it. It works now. Thank you all, really.

But I didn't get the "= true" part.

if GetPlayerStat(x, 'Active') = if ( GetPlayerStat(x, 'Active') = true ) // and...
if not GetPlayerStat(x, 'Active') = if ( GetPlayerStat(x, 'Active') = false ) // Aren't they?

At least iDante says so:
Part IIX: Boolean Statements:
Good lord this drives me insane.
Would you do this?
Code: (Pascal) [Select]
if x > y = true then ...;
Lets say that x is greater than y. The computer evaluates it as "if true = true then ..." when it makes more sense for it to evaluate it as "if true then ..."
A boolean statement goes into the condition part of an if thing.
Therefore, DO NOT DO THIS:
Code: (Pascal) [Select]
var
     bool: boolean;
begin
     if bool = true then ...;
end;
THAT IS UGLY AND MEAN TO ME!
Code: (Pascal) [Select]
if bool then ...;
IS MUCH BETTER TY!
Same with stuff like this:
Code: (Pascal) [Select]
var i: integer;
begin
     for i := 1 to 32 do
          if GetPlayerStat(i, 'active') then ...;
end;
Note: "if GetPlayerStat(i, 'active') then ...;"
I see this written as "if GetPlayerStat(i, 'active) = true then ...;" over and over again and it bugs the living shneidikkies out of me.
Please for the love of good god's goldfish, get rid of those = true's.

Note: Some of GetPlayerStat's return values are borked, be sure to make sure things are working right.

Now lets say you want to test out whether that boolean statement is NOT true.
Code: (Pascal) [Select]
if not bool then ...;
not is the keyword to use here. This basically takes over = false. Its easier to read as well.

I can't understand why this could cause an issue. :)

Whatever, here is the function, "RandomID". It returns a random active player's ID.

Code: [Select]
function RandomID: integer;
var
a: array of integer;
x, y: integer;
begin
   for x := 1 to 32 do begin
      if GetPlayerStat(x, 'Active') = true then begin
         SetArrayLength(a, y + 1);
         [y] := x;
         y := y+1;
      end;
   end;
   Result := a[Random(0, y)];
end;

Thanks again DarkCrusade, iDante and DorkeyDear.
« Last Edit: June 10, 2011, 06:23:45 pm by utkesmer »

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: How do I randomize things?
« Reply #10 on: June 10, 2011, 08:27:23 pm »
soldatcentral is no more
meh, the script is posted in the thread.

oh yeah, GetPlayerState sometimes doesn't return nice results, stick an = true onto the end of that thing. It really doesn't make sense and is dumb, but whatever.

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: How do I randomize things?
« Reply #11 on: June 11, 2011, 04:02:57 am »
But I didn't get the "= true" part.
getplayerstat returns a variant, not a bool. So it can sometimes mess up and destroy your everything

Offline utkesmer

  • Major(1)
  • Posts: 44
Re: How do I randomize things?
« Reply #12 on: June 11, 2011, 04:59:40 am »
Hmm, sorry then, you're right.

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: How do I randomize things?
« Reply #13 on: June 17, 2011, 10:47:11 pm »
That's pretty douchy of enesce to just have soldatcentral redirect to his commercial software ad site without even giving a notice.
No notice? It was on the front page of SoldatCentral since 23-09-10... 9+ months is plenty of notice.

And there is an index available for the old scripts here:
http://enesce.com/static/sc_scripts/

Offline utkesmer

  • Major(1)
  • Posts: 44
Re: How do I randomize things?
« Reply #14 on: August 23, 2011, 07:54:11 am »
Code: [Select]
function RandomID(t: integer): integer;
var
a: array of integer;
x, y: integer;
begin

 if ( t = 0 ) then begin

      for x := 1 to 32 do begin

           if ( GetPlayerStat(x, 'Active') = true ) then begin

                SetArrayLength(a, y+1);

                a[y] := x;

                y := y+1;

           end;

      end;

 end else begin

      for x := 1 to 32 do begin

           if ( GetPlayerStat(x, 'Active') = true ) and ( GetPlayerStat(x, 'Team') = t ) then begin

                SetArrayLength(a, y+1);

                a[y] := x;

                y := y+1;

           end;

      end;

 end;

 Result := a[Random(0, y)];

end;

"Team" parameter is added. I was trying to write the "!balance" command. You know, when 5v3, take a random alpha player and make it join bravo team. So I needed team parameter for this. I tried, it is working. 0 means all players for team btw.