# Get Building Count In Town

Grammar — Returns the number of buildings of type unit in town for player player using mask countMask|TechTreeCountType
FlagsNative | Function

Returns the number of buildings of the specified type in the specified town for a player.

# Arguments

  • int — Player
  • int — Town
  • string<gamelink::Unit> — Unit
  • int<preset::TechTreeCountType> — Count Mask

Returns — int

native int AIGetBuildingCountInTown(
	int player,
	int town,
	string aliasUnitType,
	int countMask,
);

Category: AI Advanced / Towns / Functions

# Examples

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:477

        }

        if (AIGetBuildingCountInTown(player, town, c_TB_CommandCenter_Alias, c_techCountCompleteOnly) < 1) {
            continue;
        }

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:481

        }

        if (AIGetBuildingCountInTown(player, town, c_TB_MissileTurret, c_techCountCompleteOnly) < 2) {
            AISetStockEx(player, town, 2, c_TB_MissileTurret, c_makeResourceDefense | c_nearDropoff, 0);
            continue;

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:485

            continue;
        }
        if (AIGetBuildingCountInTown(player, town, c_TB_Bunker, c_techCountCompleteOnly) < 1) {
            AISetStockEx(player, town, 1, c_TB_Bunker, c_makeResourceDefense | c_nearDropoff, 0);
            continue;

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:620

        }

        if (AIGetBuildingCountInTown(player, town, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
            continue;
        }

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:626

        numTowns += 1;

        if (AIGetBuildingCountInTown(player, town, c_ZB_SporeCrawler, c_techCountCompleteOnly) < 2) {
            AISetStockEx(player, town, 2, c_ZB_SporeCrawler, c_makeResourceDefense | c_nearDropoff, 0);
            continue;

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:630

            continue;
        }
        if (AIGetBuildingCountInTown(player, town, c_ZB_SpineCrawler, c_techCountCompleteOnly) < 2) {
            AISetStockEx(player, town, 2, c_ZB_SpineCrawler, c_makeResourceDefense | c_nearDropoff, 0);
            continue;

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:761

        }

        if (AIGetBuildingCountInTown(player, town, c_PB_Nexus, c_techCountCompleteOnly) < 1) {
            continue;
        }

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:766


        // make 2 pylons in each town
        if (AIGetBuildingCountInTown(player, town, c_PB_Pylon, c_techCountCompleteOnly) < 2) {
            AISetStockEx(player, town, 1, c_PB_Pylon, c_makeExpanPower, 0);
            AISetStockEx(player, town, 2, c_PB_Pylon, c_makeResourceDefense | c_nearDropoff, 0);

mods/core.sc2modbase.sc2data/TriggerLibs/MeleeLowAI.galaxy:772

        }

        if (AIGetBuildingCountInTown(player, town, c_PB_PhotonCannon, c_techCountCompleteOnly) < 2) {
            AISetStockEx(player, town, 2, c_PB_PhotonCannon, c_makeResourceDefense | c_nearDropoff, 0);
            continue;

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossChIn.galaxy:2085

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHdVH.galaxy:2084

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1007


    // make 1 pylon so we can make cannons
    pylonInTown = AIGetBuildingCountInTown(player, mainTown, c_PB_Pylon, c_techCountCompleteOnly);
    pylonCount = AITechCount(player, c_PB_Pylon, c_techCountCompleteOnly) + 1;
    if (pylonInTown == 0) {

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1015


    // make 1 cannon close to the first pylon
    cannonInTown = AIGetBuildingCountInTown(player, mainTown, c_PB_PhotonCannon, c_techCountInProgressOrBetter);
    cannonCount = AITechCount(player, c_PB_PhotonCannon, c_techCountCompleteOnly) + 1;
    if (cannonInTown == 0) {

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1071

        }

        if (AIGetBuildingCountInTown(player, town, c_PB_Nexus, c_techCountInProgressOrBetter) < 1) {
            continue;
        }

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1076


        // make 1 pylon so we can make cannons
        power = AIGetBuildingCountInTown(player, town, c_PB_Pylon, c_techCountCompleteOnly);
        if (power == 0) {
            AISetStockEx(player, town, 1, c_PB_Pylon, c_makeExpanPower, 0);

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1083


        // make 1 cannon close to the first pylon
        defense = AIGetBuildingCountInTown(player, town, c_PB_PhotonCannon, c_techCountInProgressOrBetter);
        if (defense == 0) {
            AISetStockEx(player, town, 1, c_PB_PhotonCannon, c_makeDefense, 0);

mods/liberty.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossVyHd.galaxy:2115

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/liberty.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1098

    }

    turretInTown = AIGetBuildingCountInTown(player, mainTown, c_TB_MissileTurret, c_techCountCompleteOnly);
    if (turretInTown < amount) {
        turretCount = AITechCount(player, c_TB_MissileTurret, c_techCountCompleteOnly) + 1;

mods/liberty.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1113

    //}

    commandInTown = AIGetBuildingCountInTown(player, mainTown, c_TB_CommandCenter, c_techCountCompleteOnly);
    if (commandInTown > 0) {
        orbitalCount = AITechCount(player, c_TB_OrbitalCommand, c_techCountCompleteOnly) + 1;

mods/liberty.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1152

        minLeftInTown = AIGetMineralAmountLeft(player, town);
        if (orbitalIncomplete == 0) {
            if (AIGetBuildingCountInTown(player, town, c_TB_CommandCenter, c_techCountCompleteOnly) > 0) {
                if (orbitalTotal < 3) {
                    AISetStockEx(player, town, 1, c_TB_OrbitalCommand, c_makeDefault, 0);

mods/liberty.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1027

        return;
    }
    if (AIGetBuildingCountInTown(player, mainTown, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
        return;
    }

mods/liberty.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1041

    }

    spineCrawlerInTown = AIGetBuildingCountInTown(player, mainTown, c_ZB_SpineCrawler, c_techCountCompleteOnly);
    if (spineCrawlerInTown < amount) {
        spineCrawlerCount = AITechCount(player, c_ZB_SpineCrawler, c_techCountCompleteOnly) + 1;

mods/liberty.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1048

    }

    sporeCrawlerInTown = AIGetBuildingCountInTown(player, mainTown, c_ZB_SporeCrawler, c_techCountCompleteOnly);
    if (sporeCrawlerInTown < amount + 1) {
        sporeCrawlerCount = AITechCount(player, c_ZB_SporeCrawler, c_techCountCompleteOnly) + 1;

mods/liberty.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1082

            continue;
        }
        if (AIGetBuildingCountInTown(player, town, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
            continue;
        }

mods/liberty.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1105


        // After making 2 spine crawler, create 2 spore crawlers in each non-main town
        if (AIGetBuildingCountInTown(player, town, c_ZB_SpineCrawler, c_techCountCompleteOnly) >= 2) {
            AISetStockEx(player, town, amount, c_ZB_SporeCrawler, c_makeResourceDefense, 0);
        }

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossChIn.galaxy:2085

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHdVH.galaxy:2084

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1002


    // make 1 pylon so we can make cannons
    pylonInTown = AIGetBuildingCountInTown(player, mainTown, c_PB_Pylon, c_techCountCompleteOnly);
    pylonCount = AITechCount(player, c_PB_Pylon, c_techCountCompleteOnly) + 1;
    if (pylonInTown == 0) {

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1010


    // make 1 cannon close to the first pylon
    cannonInTown = AIGetBuildingCountInTown(player, mainTown, c_PB_PhotonCannon, c_techCountInProgressOrBetter);
    cannonCount = AITechCount(player, c_PB_PhotonCannon, c_techCountCompleteOnly) + 1;
    if (cannonInTown == 0) {

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1066

        }

        if (AIGetBuildingCountInTown(player, town, c_PB_Nexus, c_techCountInProgressOrBetter) < 1) {
            continue;
        }

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1071


        // make 1 pylon so we can make cannons
        power = AIGetBuildingCountInTown(player, town, c_PB_Pylon, c_techCountCompleteOnly);
        if (power == 0) {
            AISetStockEx(player, town, 1, c_PB_Pylon, c_makeExpanPower, 0);

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1078


        // make 1 cannon close to the first pylon
        defense = AIGetBuildingCountInTown(player, town, c_PB_PhotonCannon, c_techCountInProgressOrBetter);
        if (defense == 0) {
            AISetStockEx(player, town, 1, c_PB_PhotonCannon, c_makeDefense, 0);

mods/swarm.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossVyHd.galaxy:2115

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/swarm.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1089

    }

    turretInTown = AIGetBuildingCountInTown(player, mainTown, c_TB_MissileTurret, c_techCountCompleteOnly);
    if (turretInTown < amount) {
        turretCount = AITechCount(player, c_TB_MissileTurret, c_techCountCompleteOnly) + 1;

mods/swarm.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1104

    //}

    commandInTown = AIGetBuildingCountInTown(player, mainTown, c_TB_CommandCenter, c_techCountCompleteOnly);
    if (commandInTown > 0) {
        orbitalCount = AITechCount(player, c_TB_OrbitalCommand, c_techCountCompleteOnly) + 1;

mods/swarm.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1143

        minLeftInTown = AIGetMineralAmountLeft(player, town);
        if (orbitalIncomplete == 0) {
            if (AIGetBuildingCountInTown(player, town, c_TB_CommandCenter, c_techCountCompleteOnly) > 0) {
                if (orbitalTotal < 3) {
                    AISetStockEx(player, town, 1, c_TB_OrbitalCommand, c_makeDefault, 0);

mods/swarm.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1045

        return;
    }
    if (AIGetBuildingCountInTown(player, mainTown, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
        return;
    }

mods/swarm.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1059

    }

    spineCrawlerInTown = AIGetBuildingCountInTown(player, mainTown, c_ZB_SpineCrawler, c_techCountCompleteOnly);
    if (spineCrawlerInTown < amount) {
        spineCrawlerCount = AITechCount(player, c_ZB_SpineCrawler, c_techCountCompleteOnly) + 1;

mods/swarm.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1066

    }

    sporeCrawlerInTown = AIGetBuildingCountInTown(player, mainTown, c_ZB_SporeCrawler, c_techCountCompleteOnly);
    if (sporeCrawlerInTown < amount + 1) {
        sporeCrawlerCount = AITechCount(player, c_ZB_SporeCrawler, c_techCountCompleteOnly) + 1;

mods/swarm.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1100

            continue;
        }
        if (AIGetBuildingCountInTown(player, town, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
            continue;
        }

mods/swarm.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1123


        // After making 2 spine crawler, create 2 spore crawlers in each non-main town
        if (AIGetBuildingCountInTown(player, town, c_ZB_SpineCrawler, c_techCountCompleteOnly) >= 2) {
            AISetStockEx(player, town, amount, c_ZB_SporeCrawler, c_makeResourceDefense, 0);
        }

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossChIn.galaxy:2611

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHdVH.galaxy:2613

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1039


    // make 1 pylon so we can make cannons
    pylonInTown = AIGetBuildingCountInTown(player, mainTown, c_PB_Pylon, c_techCountCompleteOnly);
    pylonCount = AITechCount(player, c_PB_Pylon, c_techCountCompleteOnly) + 1;
    if (pylonInTown == 0) {

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1047


    // make 1 cannon close to the first pylon
    cannonInTown = AIGetBuildingCountInTown(player, mainTown, c_PB_PhotonCannon, c_techCountInProgressOrBetter);
    cannonCount = AITechCount(player, c_PB_PhotonCannon, c_techCountCompleteOnly) + 1;
    if (cannonInTown == 0) {

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1103

        }

        if (AIGetBuildingCountInTown(player, town, c_PB_Nexus, c_techCountInProgressOrBetter) < 1) {
            continue;
        }

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1108


        // make 1 pylon so we can make cannons
        power = AIGetBuildingCountInTown(player, town, c_PB_Pylon, c_techCountCompleteOnly);
        if (power == 0) {
            AISetStockEx(player, town, 1, c_PB_Pylon, c_makeExpanPower, 0);

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossHigh.galaxy:1115


        // make 1 cannon close to the first pylon
        defense = AIGetBuildingCountInTown(player, town, c_PB_PhotonCannon, c_techCountInProgressOrBetter);
        if (defense == 0) {
            AISetStockEx(player, town, 1, c_PB_PhotonCannon, c_makeDefense, 0);

mods/void.sc2modbase.sc2data/TriggerLibs/Protoss/ProtossVyHd.galaxy:2644

        minPylonCount = 6;
    }
    if (AIGetBuildingCountInTown(player, c_townMain, c_PB_Pylon, c_techCountQueuedOrBetter) < minPylonCount) {
        minPylonCount = AITechCount(player, c_PB_Pylon, c_techCountQueuedOrBetter) + 1;
        AISetStockUnitNext( player, minPylonCount, c_PB_Pylon, c_stockAlways );

mods/void.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1103

    }

    turretInTown = AIGetBuildingCountInTown(player, mainTown, c_TB_MissileTurret, c_techCountCompleteOnly);
    if (turretInTown < amount) {
        turretCount = AITechCount(player, c_TB_MissileTurret, c_techCountCompleteOnly) + 1;

mods/void.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1118

    //}

    commandInTown = AIGetBuildingCountInTown(player, mainTown, c_TB_CommandCenter, c_techCountCompleteOnly);
    if (commandInTown > 0) {
        orbitalCount = AITechCount(player, c_TB_OrbitalCommand, c_techCountCompleteOnly) + 1;

mods/void.sc2modbase.sc2data/TriggerLibs/Terran/TerranHigh.galaxy:1157

        minLeftInTown = AIGetMineralAmountLeft(player, town);
        if (orbitalIncomplete == 0) {
            if (AIGetBuildingCountInTown(player, town, c_TB_CommandCenter, c_techCountCompleteOnly) > 0) {
                if (orbitalTotal < 3) {
                    AISetStockEx(player, town, 1, c_TB_OrbitalCommand, c_makeDefault, 0);

mods/void.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1055

        return;
    }
    if (AIGetBuildingCountInTown(player, mainTown, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
        return;
    }

mods/void.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1069

    }

    spineCrawlerInTown = AIGetBuildingCountInTown(player, mainTown, c_ZB_SpineCrawler, c_techCountCompleteOnly);
    if (spineCrawlerInTown < amount) {
        spineCrawlerCount = AITechCount(player, c_ZB_SpineCrawler, c_techCountCompleteOnly) + 1;

mods/void.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1076

    }

    sporeCrawlerInTown = AIGetBuildingCountInTown(player, mainTown, c_ZB_SporeCrawler, c_techCountCompleteOnly);
    if (sporeCrawlerInTown < amount + 1) {
        sporeCrawlerCount = AITechCount(player, c_ZB_SporeCrawler, c_techCountCompleteOnly) + 1;

mods/void.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1110

            continue;
        }
        if (AIGetBuildingCountInTown(player, town, c_ZB_Hatchery_Alias, c_techCountCompleteOnly) < 1) {
            continue;
        }

mods/void.sc2modbase.sc2data/TriggerLibs/Zerg/ZergHigh.galaxy:1133


        // After making 2 spine crawler, create 2 spore crawlers in each non-main town
        if (AIGetBuildingCountInTown(player, town, c_ZB_SpineCrawler, c_techCountCompleteOnly) >= 2) {
            AISetStockEx(player, town, amount, c_ZB_SporeCrawler, c_makeResourceDefense, 0);
        }

mods/warcoop/warmeleeai.sc2modbase.sc2data/TriggerLibs/Common_ai.galaxy:29

    int mask=c_techCountQueuedOrBetter;
    if(only_done){mask=c_techCountCompleteOnly;}
    return AIGetBuildingCountInTown(player, townid, unitid,mask);
}
//============================================================================