Author Topic: AlivePlayers  (Read 1517 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
AlivePlayers
« on: May 26, 2007, 08:03:02 pm »
I'm having issues with getting the number of alive players... I'm trying not to use for loops.
Code: [Select]
var
  AlivePlayers,AfterJoinTeam,AfterKill,Old: byte;

procedure AppOnIdle(Ticks: integer);
var
  i: byte;
begin
  if AfterJoinTeam <> 0 then begin
    if GetPlayerStat(AfterJoinTeam,'Alive') = true then AlivePlayers := AlivePlayers + 1;
    AfterJoinTeam := 0;
  end;
  if AfterKill <> 0 then begin
    for i := 1 to NumPlayers do if Target[i] = AfterKill then begin
      Command('/setteam ' + InttoStr(i));
Command('/say ' + InttoStr(i) + ' respawned due to his killer, ' + InttoStr(AfterKill) + ', dieing.');
    end;
    AfterKill := 0;
  end;
  if Old <> AlivePlayers then begin
    Command('/say AlivePlayers = ' + InttoStr(AlivePlayers) + ' / ' + InttoStr(NumPlayers));
    Old := AlivePlayers;
  end;
end;

procedure OnJoinTeam(ID, Team: byte);
begin
  AfterJoinTeam := ID;
end;

procedure OnLeaveGame(ID, Team: byte; Kicked: boolean);
begin
  if GetPlayerStat(ID,'Alive') = true then AlivePlayers := AlivePlayers - 1;
end;

procedure OnPlayerKill(Killer, Victim: byte; Weapon: string);
var
  i: byte;
begin
  AlivePlayers := AlivePlayers - 1;
  AfterKill := Victim;
end;

procedure OnPlayerRespawn(ID: byte);
begin
  AlivePlayers := AlivePlayers + 1;
end;

It may have something to do with when the script does Command('setteam_ _') and that not calling OnPlayerRespawn, but whenever I do specific tests, it seems to do fine. Its really hard to find the cause with bots. AlivePlayers is always correct or too high, never too low.

(Deathmatch)

I also suggest having AlivePlayers or something as an implemented byte (but then to make it fair, you would need alpha, bravo, charlie, and delta alive players too, but whatever)
« Last Edit: May 26, 2007, 08:08:24 pm by DorkeyDear »

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: AlivePlayers
« Reply #1 on: May 26, 2007, 09:32:18 pm »
I think the script isn't very consistent the way you are doing it. AppOnIdle is supposed to get called each second, so if 2 players die at the same time only one would be counted i guess. Also OnLeaveGame isn't very effective, because sometimes players get diconnected without calling any event.
Why do you try to avoid for loops. Making a loop for 32 players won't cause any performance issue. I think you must use the for loop each time you want to count the players alive.
urraka

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: AlivePlayers
« Reply #2 on: May 26, 2007, 11:02:51 pm »
GetPlayerStat has an alive option...

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: AlivePlayers
« Reply #3 on: May 27, 2007, 12:00:59 am »
GetPlayerStat has an alive option...

He already knows.. did you take a look at the code? That option only informs if one player is alive or not.. not how many players are alive in the moment.
urraka

Offline Quantifier

  • Major
  • *
  • Posts: 70
Re: AlivePlayers
« Reply #4 on: May 27, 2007, 03:31:25 am »
First, don't use NumPlayers as number of loop iterations, it will work reliably only until for the first time someone leaves the server. MaxPlayers is better, but still won't catch admins that joined on full server.
Second, you never define target here, or even assign value to it.
Third, afterkill holds the value of victim, then in apponidle you say it's a killer. I guess it has to do with missing code involving target array?

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: AlivePlayers
« Reply #5 on: May 27, 2007, 04:47:19 am »
Code: [Select]
function AlivePlayers: byte;
var i: byte;
begin
  result:=0;
  for i:=1 to 32 do
    If (GetPlayerStat(i,'Alive')=true) and (GetPlayerStat(i,'Active')=true) then
      result:=result + 1;
end;

it's that easy ;Q
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline KeYDoN

  • Major
  • *
  • Posts: 60
Re: AlivePlayers
« Reply #6 on: May 27, 2007, 11:07:21 am »
avarax: you should exclude specs ;)

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: AlivePlayers
« Reply #7 on: May 27, 2007, 03:24:35 pm »
afaik (GetPlayerStat(i,'Alive')=true) excludes them
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: AlivePlayers
« Reply #8 on: May 27, 2007, 05:49:05 pm »
Heh, I knew how to use the for loops. I just dislike using them when I don't need to. I guess I will be needing them, thanks.
(Now I am needing to know when each procedure is called so I can make the for loop be called after the player respawns or leaves or whatever..)
OnLeaveGame is not called when a player is disconnected? :'( It should be...

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: AlivePlayers
« Reply #9 on: May 27, 2007, 05:53:02 pm »
You don't need to know that, just call Avarax function each time you need to know the value of AlivePlayers.
urraka

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: AlivePlayers
« Reply #10 on: May 27, 2007, 05:55:59 pm »
Yeah, or I could use it right before I need it. That works too. Smarter idea :)