How do I count the number of players?

Started by 1337, March 08, 2010, 05:05:44 AM

Previous topic - Next topic

1337

I want to know how many total players are in the game... Still working hard on the 1337 AI but I really need this (and how to count the number of units in production at a building).


I found these in <SC2>/Mods/Core.SC2Mode/Base.SC2Data/TriggerLibs/GameData/Game.galaxy:


//  EPlayerType
const int c_playerTypeNone = 0;
const int c_playerTypeUser = 1;
const int c_playerTypeComputer = 2;
const int c_playerTypeNeutral = 3;
const int c_playerTypeHostile = 4;
const int c_playerTypeReferee = 5;
const int c_playerTypeSpectator = 6;



But I can't for the life of me find a function to get the enum value given a player number.

hd

#1
Could probably do something like this:

int howManyPlayers(int player)
{
    int iCount = player;

    while (iCount < 12)
    {
        // echo a msg in game as to the value of PlayerRace() on a null value, then filter out the null value.
        if (PlayerRace(iCount) == "Prot" || PlayerRace(iCount) == "Terr" || PlayerRace(iCount) == "Zerg")
        {
            iCount = iCount + 1;
        }
        else
        {
            TriggerDebugOutput(1, StringToText("Invalid race found @ " + IntToString(iCount)), true);
            return iCount;
        }

    return iCount;
}

1337

#2
Thank you so much HD that was exactly what I needed. Here's my final version:
//Count players
i = 1;
j = 16;   //this is the max. setting it to 17+ generates an error
TOTAL_PLAYERS = 0;

while(i < j)
{
   if(PlayerRace(i) == "Prot" || PlayerRace(i) == "Terr" || PlayerRace(i) == "Zerg")
   {
      TOTAL_PLAYERS = TOTAL_PLAYERS + 1;
   }
   //TriggerDebugOutput(1, StringToText("Player " + IntToString(i) + " color index: "+IntToString(PlayerGetColorIndex(i, false))), true);
   i = i + 1;
}

TriggerDebugOutput(1, StringToText("TOTAL_PLAYERS: "+IntToString(TOTAL_PLAYERS)), true);

As noted in the comment, I cannot loop higher than i = 15 without generating runtime errors. IDK why.

hd

because the game isn't going to support more than that. Most likely it's only going to support up to 8 or possibly 12 people.

When you call PlayerRace(int player), the function has access to information we don't. Like a const cap of the possible amount of players. So, when you pass a number higher than the cap, it returns an error.

Heinermann

Why do you people insist on doing things the hard way? Seriously.

int numPlayers = AIGetNumEnemies(player) + AIGetNumAllies(player) + 1;

siman

Quote from: Heinermann on March 10, 2010, 11:45:14 AM
Why do you people insist on doing things the hard way? Seriously.

int numPlayers = AIGetNumEnemies(player) + AIGetNumAllies(player) + 1;


Did you try that your own?
I think AIGetNumEnemies is broken, it does not return the correct value for AI players