Author Topic: Random Player  (Read 1234 times)

0 Members and 1 Guest are viewing this topic.

Offline Buddhaa

  • Major(1)
  • Posts: 6
Random Player
« on: May 27, 2009, 06:04:13 am »
Hi,

Just hit a bit of a wall with my limited scripting skills and I'm in need of a bit of assistance. I'm trying to write a script that will perform a specific command to a random player in the server. E.g. When a player types !kill, it will kill a random player. Any help would be greatly appreciated.

Peace,

Buddhaa  :P

Offline m00`

  • Soldier
  • **
  • Posts: 169
  • hai guys
Re: Random Player
« Reply #1 on: May 27, 2009, 06:55:44 am »
well if you have a 10 player server you can do something like

function OnPlayerCommand(ID: Byte; Text: string): boolean;
var PlayerIDlolz: byte;
begin
if  (Text = '/COMMANDLOLOLOL') then begin
  PlayerIDlolz := random(1,10);
  <<<BLAH BLAH BLAH YOUR CODE HEREEEEEE>>>;
end;
end;

oh yeah make sure you use the PlayerIDlolz variable in place of the ID parameter in a function thingymabob

oh yeah dont trust me too much because im s**t at coding
cool

Offline y0uRd34th

  • Camper
  • ***
  • Posts: 325
  • [i]Look Signature![/i]
Re: Random Player
« Reply #2 on: May 27, 2009, 06:59:48 am »
Random(1,11) not Random(1,10) someone said that to me, I forgot why but always add to the seconds number 1 so the result can be 1-10 otherwise it will be 1-9, hope you understand that :D

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Random Player
« Reply #3 on: May 27, 2009, 07:29:34 am »
function randPlayer: byte; var a: array[0..31] of byte; i,x: byte;
begin
for i:=1 to 32 do if getplayerstat(i,'active') then begin a[x ]:=i; x:=x+1; end;
result:=a[random(0,x)];
end;

Not tested.
« Last Edit: May 30, 2009, 02:28:12 am by tk »

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: Random Player
« Reply #4 on: May 27, 2009, 08:25:55 am »
or you could go cheap and do something like

r := 0;
while getplayerstat(r,'active') = false do
  r := random(1,32);

(m00's code might break if someone with the ID greater than highest has left the game)

Offline m00`

  • Soldier
  • **
  • Posts: 169
  • hai guys
Re: Random Player
« Reply #5 on: May 27, 2009, 08:30:42 am »
lol I just realised how bad mine is
cool

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Random Player
« Reply #6 on: May 27, 2009, 08:36:06 am »
Code: [Select]
function RandomPlayer(): byte;
begin
  if (NumPlayers = 0) then
    exit;
  repeat
    Result := Random(1, 33);
  until (GetPlayerStat(Result, 'Active') = true);
end;
Will return 0 if nobody is in-game currently.

Offline tk

  • Soldier
  • **
  • Posts: 235
Re: Random Player
« Reply #7 on: May 27, 2009, 08:51:48 am »
Random function is not too random. When there is one player on the server, it may do hundreds of loops.

Offline Toumaz

  • Veteran
  • *****
  • Posts: 1904
Re: Random Player
« Reply #8 on: May 27, 2009, 02:11:11 pm »
Whoopedoo - not that I've tested it, but it ought to work.

edit: okay that was just about the same way tk did his but what the hell

Code: [Select]
function getRandomPlayerID(): byte;
var
    players: array[1..32] of byte;
    playersNumber: byte;
    i: byte;
begin
    // if there isn't a player in-game, then bail out
    if NumPlayers = 0 then begin
        Result := 0;
        exit;
    end;

    // get rrrreeaadyyy to rrrrumbleee
    playersNumber := 0;

    // loop through all player ID 'slots'
    for i := 1 to 32 do begin
        // if there's no player with this ID in-game,
        // then simply skip to the next ID in the loop
        if not getPlayerStat(i, 'active') then continue;
       
        // increment the numbers of players we've found...
        playersNumber := playersNumber + 1;

        // ...and tuck the latest player ID onto that array
        players[playersNumber] := i;
    end;

    // now just return a random ID from the aforementioned array
    // and you ought to be all peachy-keen!
    Result := players[Random(1, playersNumber + 1)];
end;
« Last Edit: May 27, 2009, 02:13:31 pm by Toumaz »

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Random Player
« Reply #9 on: May 27, 2009, 08:58:18 pm »

Offline Buddhaa

  • Major(1)
  • Posts: 6
Re: Random Player
« Reply #10 on: May 28, 2009, 05:46:49 am »
Thanks heaps for the replies. They helped loads.