MNoya / BuildingHelper

Library for RTS & TD Dota Custom Games
GNU General Public License v3.0
37 stars 14 forks source link

Enabling bots to build with Build #11

Closed Arildlil closed 8 years ago

Arildlil commented 8 years ago

I noticed that adding a few lines (marked with '-- Added {' and '-- }') in BuildingHelper.lua enabled bots to construct buildings the usual way:

function BuildingHelper:BuildCommand(args)
    local playerID = args['PlayerID']
    local x = args['X']
    local y = args['Y']
    local z = args['Z']
    local location = Vector(x, y, z)
    local queue = args['Queue'] == 1
    local builder = EntIndexToHScript(args['builder']) --activeBuilder
    local name = builder:GetUnitName()
    local builders = {}
    local idle_builders = {}
    local entityList = PlayerResource:GetSelectedEntities(playerID)

    -- Filter all the selected builders
    for k,entIndex in pairs(entityList) do
        local unit = EntIndexToHScript(entIndex)
        if unit:GetUnitName() == name then
            if unit:IsIdle() then
                table.insert(idle_builders, unit)
            end
            table.insert(builders, unit)
        end
    end

    -- Added {
    -- Since the bot didn't have a selection, this was required as its selection was empty.
    if args.bot and #builders == 0 then
        table.insert(builders, builder)
    end
    -- }

    -- First select from idle builders
    if #idle_builders > 0 then
        builder = GetClosestToPosition(idle_builders, location)
    else
        builder = GetClosestToPosition(builders, location)
    end

    -- Cancel current action
    if not queue then
        ExecuteOrderFromTable({UnitIndex = builder:GetEntityIndex(), OrderType = DOTA_UNIT_ORDER_STOP, Queue = false}) 
    end

    BuildingHelper:AddToQueue(builder, location, queue)
end

and changing the last line in BuildingHelper:AddBuilding() from CustomGameEventManager:Send_ServerToPlayer(player, "building_helper_enable", event) to

if not keys.bot then      
   CustomGameEventManager:Send_ServerToPlayer(player, "building_helper_enable", event)
end

You can then order the bot to build with

function ConstructBuilding(worker, ability, position)
    local playerID = worker:GetPlayerOwnerID()
    -- I've only tried setting Queue to 0, might be better at 1 if queuing
    local buildArgs = {
        builder = worker:GetEntityIndex(),
        Queue = 0,
        PlayerID = playerID,
        X = position.x,
        Y = position.y,
        Z = position.z,
        bot = true
    }

    Build({caster=worker, ability=ability})
    BuildingHelper:BuildCommand(buildArgs)
end

I havn't tried to queue up several constructions, so I'm not sure how that would work with setting Queue to 1, but there's at least no problem in constructing several buildings for a bot when the queue is empty.

MNoya commented 8 years ago

I'll try this, looks promising! Thank you

MNoya commented 8 years ago

Added in 1.2.7 69675a4e0c722b732409626a9ac26191200ddf35

Usage: BuildingHelper:OrderBuildingConstruction(builder, ability, position)