Esempio n. 1
0
def runWorkerLogicMars(worker, workersInformation, gc):

    if not gc.can_sense_unit(worker.workerUnitID) or gc.unit(worker.workerUnitID).location.is_in_garrison():
        workersInformation.marsWorkersList.remove(worker)
        return
    unitLocation = gc.unit(worker.workerUnitID).location.map_location()

    directions = list(bc.Direction)
    random.shuffle(directions)

      # If there is karbonite nearby, then try to harvest it 
    for direction in directions:
        adjacentLocation = unitLocation.add(direction)
        # Need to try/catch because if adjacentLocation is not on map, then karbonite_at() throws exception
        try:
            if gc.karbonite_at(adjacentLocation) > 0:
                if gc.can_harvest(worker.workerUnitID, direction):
                    gc.harvest(worker.workerUnitID, direction)
                    return
        except Exception as e:
            continue

    # Search karbonite
    for direction in directions:
        if gc.is_move_ready(worker.workerUnitID):
            if gc.can_move(worker.workerUnitID, direction):
                # gc.move_robot(worker.workerUnitID, direction)
                move.goto(gc, worker.workerUnitID, unitLocation.add(direction))
                return
Esempio n. 2
0
def runWorkerLogic(worker, unitInfo, workersInformation, gc):
    # Current location of the unit

    # every 150 turns increase max number of factories
    if gc.round() % 150:
        workersInformation.maxFactories += 1
    # Randomize array of directions each turn
    directions = list(bc.Direction)
    random.shuffle(directions)

    if not gc.can_sense_unit(worker.workerUnitID) or gc.unit(worker.workerUnitID).location.is_in_garrison():
        workersInformation.workersList.remove(worker)
        return

    if gc.team() == bc.Team.Red:
        enemyTeam = bc.Team.Blue
    else: 
        enemyTeam = bc.Team.Red 

    unitLocation = gc.unit(worker.workerUnitID).location.map_location()
    nearbyUnits = gc.sense_nearby_units(unitLocation, 3)
    nearbyEnemyUnits = gc.sense_nearby_units_by_team(unitLocation, gc.unit(worker.workerUnitID).vision_range, enemyTeam)

    #flee from enemy army

    for nearbyEnemyUnit in nearbyEnemyUnits:
        if nearbyEnemyUnit.unit_type == bc.UnitType.Ranger or nearbyEnemyUnit.unit_type == bc.UnitType.Knight or nearbyEnemyUnit.unit_type == bc.UnitType.Mage:
            move.gofrom(gc,worker.workerUnitID,nearbyEnemyUnit.location.map_location())
            worker.hasPlan = False
            return


    # If there are less than maxWorkers workers, then try to replicate
    if unitInfo.workerCount < workersInformation.maxWorkers:
        for direction in directions:
            if gc.can_replicate(worker.workerUnitID, direction):
                gc.replicate(worker.workerUnitID, direction)
                for unit in gc.sense_nearby_units(unitLocation.add(direction), 0):
                    if unit.unit_type == bc.UnitType.Worker:
                        wrk = Worker(unit.id)
                        workersInformation.workersList.append(wrk)
                        if gc.round() < 50:
                            setWorkerPlan(wrk,workersInformation,gc)
                return

    # go according to plan
    if worker.hasPlan:
        move.goto(gc, worker.workerUnitID, worker.destination)
        if worker.destination.distance_squared_to((gc.unit(worker.workerUnitID)).location.map_location()) < 5:
            worker.hasPlan = False
        return

    # building a factory
    if worker.buildsFactory:
        # If the factory the worker is building is not finished, build it
        if gc.can_build(worker.workerUnitID, worker.factoryBuildID):
            gc.build(worker.workerUnitID, worker.factoryBuildID)
            return
        else:
            worker.buildsFactory = False
            return

    # If there are less than maxFactories factories, then try to blueprint a factory
    if unitInfo.factoryCount < workersInformation.maxFactories:
        if gc.karbonite() > bc.UnitType.Factory.blueprint_cost():
            for direction in directions:
                if gc.can_blueprint(worker.workerUnitID, bc.UnitType.Factory, direction):
                    gc.blueprint(worker.workerUnitID, bc.UnitType.Factory, direction)
                    worker.buildsFactory=True
                    for unit in gc.sense_nearby_units(unitLocation.add(direction), 0):
                        if unit.unit_type==bc.UnitType.Factory:
                            worker.factoryBuildID = unit.id
                    return

    # If there is no rocket, then build one
    if (unitInfo.rocketCount == 0 and gc.round()>200) or gc.round()>650:   #don't know about the constant?
        if gc.karbonite() > bc.UnitType.Rocket.blueprint_cost():
            for direction in directions:
                if gc.can_blueprint(worker.workerUnitID, bc.UnitType.Rocket, direction):
                    gc.blueprint(worker.workerUnitID, bc.UnitType.Rocket, direction)
                    worker.buildsFactory = True
                    for unit in gc.sense_nearby_units(unitLocation.add(direction), 0):
                        if unit.unit_type == bc.UnitType.Rocket:
                            worker.factoryBuildID = unit.id
                        return  

    # If there is karbonite nearby, then try to harvest it 
    for direction in directions:
        adjacentLocation = unitLocation.add(direction)
        # Need to try/catch because if adjacentLocation is not on map, then karbonite_at() throws exception
        try:
            if gc.karbonite_at(adjacentLocation) > 0:
                if gc.can_harvest(worker.workerUnitID, direction):
                    gc.harvest(worker.workerUnitID, direction)
                    workersInformation.currentKarbonite[gc.unit(worker.workerUnitID).location.map_location().add(direction).x][gc.unit(worker.workerUnitID).location.map_location().add(direction).x] -= 3
                    return
        except Exception as e:
            continue

    # Search karbonite
    for direction in directions:
        if gc.is_move_ready(worker.workerUnitID):
            if gc.can_move(worker.workerUnitID, direction):
                # gc.move_robot(worker.workerUnitID, direction)
                move.goto(gc, worker.workerUnitID, unitLocation.add(direction))
                return


    # repairing comes last - least important
    for nearbyUnit in nearbyUnits:
        # If there are damaged factories nearby, then try to repair them
        if gc.can_repair(worker.workerUnitID, nearbyUnit.id):
            gc.repair(worker.workerUnitID, nearbyUnit.id)
            return
    return

    # if everything failed, try to just go randomly...
    for direction in directions:
        if gc.is_move_ready(worker.workerUnitID):
            if gc.can_move(worker.workerUnitID, direction):
                # gc.move_robot(worker.workerUnitID, direction)
                move.goto(gc, worker.workerUnitID, unitLocation.add(direction))
                return
Esempio n. 3
0
def runHealerLogic(unit, unitInfo, mapInfo, gc):
    # If healer is in garrison or space, then do nothing
    if not unit.location.is_on_map():
        return

    # Current location of the unit
    unitLocation = unit.location.map_location()

    if gc.is_heal_ready(unit.id):
        if not gc.is_move_ready(unit.id):
            return

        if unit.location.map_location().planet == bc.Planet.Earth:
            FriendlyUnitLocation = findFriendlyUnitLocation(
                gc, unit, unitInfo.earthFriendlyUnitsLocation)
        else:
            FriendlyUnitLocation = findFriendlyUnitLocation(
                gc, unit, unitInfo.marsFriendlyUnitsLocation)

        if not FriendlyUnitLocation is None:
            direction = unitLocation.direction_to(FriendlyUnitLocation)
            if gc.can_move(unit.id, direction):
                #gc.move_robot(unit.id, direction)
                move.goto(gc, unit.id, unitLocation.add(direction))
                return
        return

    # Randomize array of directions each turn
    directions = list(bc.Direction)
    random.shuffle(directions)

    nearbyAlliedUnits = gc.sense_nearby_units_by_team(unitLocation,
                                                      unit.attack_range(),
                                                      unitInfo.myTeam)
    for nearbyAlliedUnit in nearbyAlliedUnits:
        # If there are wounded allied units nearby, then try to heal them
        if gc.can_heal(unit.id, nearbyAlliedUnit.id):
            gc.heal(unit.id, nearbyAlliedUnit.id)
            return
    '''
    visibleAlliedUnits = gc.sense_nearby_units_by_team(unitLocation, unit.vision_range, unitInfo.myTeam)
    for visibleAlliedUnit in visibleAlliedUnits:
        # If there are visible allied units nearby, then try to move closer to them
        direction = unitLocation.direction_to(visibleAlliedUnit.location.map_location())
        if gc.is_move_ready(unit.id):
            if gc.can_move(unit.id, direction):
                #gc.move_robot(unit.id, direction)
                move.goto(gc, unit.id, unitLocation.add(direction))
                return
    '''

    if not gc.is_move_ready(unit.id):
        return

    # If there are visible enemy units nearby, just run!
    enemies = gc.sense_nearby_units_by_team(unitLocation, unit.vision_range,
                                            unitInfo.enemyTeam)
    if len(enemies) > 0:
        enemies = sorted(enemies,
                         key=lambda x: x.location.map_location().
                         distance_squared_to(unitLocation))
        enemy_loc = enemies[0].location.map_location()
        reverseEnemyDirection = enemy_loc.direction_to(unitLocation)
        if gc.can_move(unit.id, reverseEnemyDirection):
            #gc.move_robot(unit.id, reverseEnemyDirection)
            move.goto(gc, unit.id, unitLocation.add(reverseEnemyDirection))
            return

    # Try to move to closest friendly unit
    if unit.location.map_location().planet == bc.Planet.Earth:
        FriendlyUnit = findFriendlyUnitLocation(
            gc, unit, unitInfo.earthFriendlyWoundedUnits)
    else:
        FriendlyUnit = findFriendlyUnitLocation(
            gc, unit, unitInfo.marsFriendlyWoundedUnits)

    if not FriendlyUnit is None:
        direction = unitLocation.direction_to(
            FriendlyUnit.location.map_location())
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    # Try to move to starting enemy location
    for startingEnemyLocation in mapInfo.startingEnemyLocations:
        direction = unitLocation.direction_to(startingEnemyLocation)
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    # Move randomly
    for direction in randomDirections:
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    return
Esempio n. 4
0
def runKnightLogic(unit, unitInfo, mapInfo, gc):
    # If knight is in garrison, then do nothing
    if not unit.location.is_on_map():
        return

    # Current location of the unit
    unitLocation = unit.location.map_location()

    if not gc.is_attack_ready(unit.id):
        if not gc.is_move_ready(unit.id):
            return

        if unit.location.map_location().planet == bc.Planet.Earth:
            FriendlyUnitLocation = findFriendlyUnitLocation(
                gc, unit, unitInfo.earthFriendlyUnitsLocation)
        else:
            FriendlyUnitLocation = findFriendlyUnitLocation(
                gc, unit, unitInfo.marsFriendlyUnitsLocation)

        if not FriendlyUnitLocation is None:
            direction = unitLocation.direction_to(FriendlyUnitLocation)
            if gc.can_move(unit.id, direction):
                #gc.move_robot(unit.id, direction)
                move.goto(gc, unit.id, unitLocation.add(direction))
                return
        return

    # Randomize array of directions each turn
    randomDirections = move.directions
    random.shuffle(randomDirections)

    # If there are enemy units in ability range, then try to javelin them
    if unit.is_ability_unlocked():
        if gc.is_javelin_ready(unit.id):
            javelinRangeEnemyUnits = gc.sense_nearby_units_by_team(
                unitLocation, unit.ability_range(), unitInfo.enemyTeam)
            for javelinRangeEnemyUnit in javelinRangeEnemyUnits:
                if gc.can_javelin(unit.id, javelinRangeEnemyUnit.id):
                    gc.javelin(unit.id, javelinRangeEnemyUnit.id)
                    return

    # If there are enemy melee range, then try to attack them
    meleeRangeEnemyUnits = gc.sense_nearby_units_by_team(
        unitLocation, unit.attack_range(), unitInfo.enemyTeam)
    for meleeRangeEnemyUnit in meleeRangeEnemyUnits:
        if gc.can_attack(unit.id, meleeRangeEnemyUnit.id):
            gc.attack(unit.id, meleeRangeEnemyUnit.id)
            return
    '''
    visibleEnemyUnits = gc.sense_nearby_units_by_team(unitLocation, unit.vision_range, unitInfo.enemyTeam)
    for visibleEnemyUnit in visibleEnemyUnits:
        # If there are visible enemy units nearby, then try to move closer to them
        direction = unitLocation.direction_to(visibleEnemyUnit.location.map_location())
        if gc.is_move_ready(unit.id):
            if gc.can_move(unit.id, direction):
                #gc.move_robot(unit.id, direction)
                move.goto(gc, unit.id, unitLocation.add(direction))
                return
    '''

    if not gc.is_move_ready(unit.id):
        return

    # Try to move to closest enemy unit
    if unit.location.map_location().planet == bc.Planet.Earth:
        EnemyUnitLocation = findEnemyUnitLocation(
            gc, unit, unitInfo.earthVisibleEnemyUnitsLocation)
    else:
        EnemyUnitLocation = findEnemyUnitLocation(
            gc, unit, unitInfo.marsVisibleEnemyUnitsLocation)

    if not EnemyUnitLocation is None:
        direction = unitLocation.direction_to(EnemyUnitLocation)
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    # Try to move to starting enemy location
    for startingEnemyLocation in mapInfo.startingEnemyLocations:
        direction = unitLocation.direction_to(startingEnemyLocation)
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    # Move randomly
    for direction in randomDirections:
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    return
Esempio n. 5
0
def runRangerLogic(unit, unitInfo, mapInfo, gc):
    # If ranger is in garrison or space, then do nothing
    if not unit.location.is_on_map():
        return

    if unit.ranger_is_sniping():
        return

    # Current location of the unit
    unitLocation = unit.location.map_location()

    if not gc.is_attack_ready(unit.id):
        if not gc.is_move_ready(unit.id):
            return

        if unit.location.map_location().planet == bc.Planet.Earth:
            FriendlyUnitLocation = findFriendlyUnitLocation(
                gc, unit, unitInfo.earthFriendlyUnitsLocation)
        else:
            FriendlyUnitLocation = findFriendlyUnitLocation(
                gc, unit, unitInfo.marsFriendlyUnitsLocation)

        if not FriendlyUnitLocation is None:
            direction = unitLocation.direction_to(FriendlyUnitLocation)
            if gc.is_move_ready(unit.id):
                if gc.can_move(unit.id, direction):
                    #gc.move_robot(unit.id, direction)
                    move.goto(gc, unit.id, unitLocation.add(direction))
                    return
        return

    # Randomize array of directions each turn
    randomDirections = move.directions
    random.shuffle(randomDirections)

    # Attack the closest units
    nearbyEnemyUnits = gc.sense_nearby_units_by_team(unitLocation,
                                                     unit.attack_range(),
                                                     unitInfo.enemyTeam)
    for nearbyEnemyUnit in nearbyEnemyUnits:
        if gc.can_attack(unit.id, nearbyEnemyUnit.id):
            gc.attack(unit.id, nearbyEnemyUnit.id)
            return

    # If there are enemy units in ability range, then try to snipe them
    if unit.is_ability_unlocked():
        if gc.is_begin_snipe_ready(unit.id):
            if unit.location.map_location().planet == bc.Planet.Earth:
                visibleEnemyUnitsLocation = unitInfo.earthVisibleEnemyUnitsLocation
            else:
                visibleEnemyUnitsLocation = unitInfo.marsVisibleEnemyUnitsLocation

            for eachEnemyUnitLocation in visibleEnemyUnitsLocation:
                '''
                # Snipe only knights, rangers and mages
                if eachEnemyUnitLocation.unit_type == bc.UnitType.Worker or eachEnemyUnitLocation.unit_type == bc.UnitType.Rocket or eachEnemyUnitLocation.unit_type == bc.UnitType.Factory:
                    break
                '''
                if gc.can_begin_snipe(unit.id, eachEnemyUnitLocation):
                    gc.begin_snipe(unit.id, eachEnemyUnitLocation)
                    return
    '''
    visibleEnemyUnits = gc.sense_nearby_units_by_team(unitLocation, unit.vision_range, unitInfo.enemyTeam)
    for visibleEnemyUnit in visibleEnemyUnits:
        # If there are visible enemy units nearby, then try to move closer to them
        direction = unitLocation.direction_to(visibleEnemyUnit.location.map_location())
        if gc.is_move_ready(unit.id):
            if gc.can_move(unit.id, direction):
                #gc.move_robot(unit.id, direction)
                move.goto(gc, unit.id, unitLocation.add(direction))
                return
    '''

    if not gc.is_move_ready(unit.id):
        return

    # Try to move to closest enemyUnit
    if unit.location.map_location().planet == bc.Planet.Earth:
        EnemyUnitLocation = findEnemyUnitLocation(
            gc, unit, unitInfo.earthVisibleEnemyUnitsLocation)
    else:
        EnemyUnitLocation = findEnemyUnitLocation(
            gc, unit, unitInfo.marsVisibleEnemyUnitsLocation)

    if not EnemyUnitLocation is None:
        direction = unitLocation.direction_to(EnemyUnitLocation)
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    # Try to move to starting enemy location
    for startingEnemyLocation in mapInfo.startingEnemyLocations:
        direction = unitLocation.direction_to(startingEnemyLocation)
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    # Move randomly
    for direction in randomDirections:
        if gc.can_move(unit.id, direction):
            #gc.move_robot(unit.id, direction)
            move.goto(gc, unit.id, unitLocation.add(direction))
            return

    return