Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ScaryBot.h"
- #include <string>
- #include <sstream>
- #include <iostream>
- using namespace std;
- using namespace BWAPI;
- void ScaryBot::onStart()
- {
- Broodwar->printf("ScaryBot");
- Broodwar->setLocalSpeed(FB_LOCAL_SPEED);
- // Find the centre of the map.
- mapCentre = Position(Broodwar->mapWidth() * TILE_SIZE / 2,
- Broodwar->mapHeight() * TILE_SIZE / 2);
- //pointOne = Position(Broodwar->mapWidth() * TILE_SIZE / 2,0);
- halfWidth = (Broodwar->mapWidth() * TILE_SIZE) / 2;
- halfHeight = (Broodwar->mapWidth() * TILE_SIZE) / 2;
- halfwayCentre = Position(halfWidth /2,halfHeight);
- pointTwo = Position(halfWidth/2,
- halfHeight + (TILE_SIZE * 5));
- refHeight1 = halfHeight + TILE_SIZE * 11;
- referencePos1 = Position(halfWidth-TILE_SIZE*10, refHeight1);
- // We don't have a target until we've scouted one out.
- target = NULL;
- // Move all units to the centre of the map.
- set<Unit *> units = Broodwar->self()->getUnits();
- unsigned int counter = 0;
- for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
- {
- if(counter < (units.size()/2))
- {
- unit1.insert(*unit);
- }
- else if(counter >= (units.size()/2))
- {
- unit2.insert(*unit);
- }
- Position currentPosition = (*unit)->getPosition();
- counter++;
- }
- //start scouting.
- moveToPos(unit1, halfwayCentre);
- //moveToPos(unit1);
- moveToPos(unit2, pointTwo);
- makeFormation();
- }
- void ScaryBot::makeFormation()
- {
- set<Unit *> units = Broodwar->self()->getUnits();
- int tileCounter = 1;
- // Sort the units by distance to the target
- for(set<Unit *>::iterator unit = units.begin(); unit!=units.end(); unit++)
- {
- set<Unit *> individual;
- individual.insert(*unit);
- moveToPos(individual, referencePos1);
- referencePos1 = Position(halfWidth-TILE_SIZE*10, refHeight1 - (TILE_SIZE*tileCounter + 40));
- tileCounter++;
- }
- }
- void ScaryBot::onFrame()
- {
- /* If we haven't got a target or our target has been destroyed,
- * try to aquire a new target. If a new target cannot be found,
- * start scouting again.
- */
- if (target == NULL || !target->exists())
- {
- if (tryAquireTarget())
- {
- attackTarget();
- }
- else
- {
- //moveToCentre();
- //moveToPointOne(unit1);
- //moveToPointTwo(unit2);
- }
- }
- drawMarkings();
- //Broodwar->drawCircleMap(halfwayCentre.x(), halfwayCentre.y(), 50, 6, true);
- }
- /**
- * Target the enemy that causes our units to travel the least to
- * attack. If no enemies are visible, do nothing.
- */
- Unit *ScaryBot::tryAquireTarget()
- {
- // All the enemy units we can see.
- set<Unit *> emenies = Broodwar->enemy()->getUnits();
- // If we can't see any enemies, there is no closest enemy.
- if (emenies.empty())
- {
- return NULL;
- }
- // All of our units.
- set<Unit *> units = Broodwar->self()->getUnits();
- // The closest enemy found so far.
- Unit *closestEnemy;
- /* The shortest total distance our units will have travel to
- * attack an enemy unit.
- */
- double leastTotalTravelDistance = numeric_limits<double>::max();
- for (set<Unit *>::iterator enemy = emenies.begin(); enemy != emenies.end(); enemy++)
- {
- // The total distance our units will have travel to attack enemy.
- double totalTraveldistance = 0.0;
- for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
- {
- /* The distance our unit has to travel to attack 'enemy'.
- * If less than zero, our unit can attack from its
- * current position.
- */
- double travelDistance = (*unit)->getDistance(*enemy) - FB_DRAGOON_WEAPON_RANGE;
- if (travelDistance > 0)
- {
- totalTraveldistance += travelDistance;
- }
- }
- /* Would attacking 'enemy' cause our units to travel less than all
- * other enemies so far?
- */
- if (totalTraveldistance <= leastTotalTravelDistance)
- {
- closestEnemy = *enemy;
- leastTotalTravelDistance = totalTraveldistance;
- }
- }
- return target = closestEnemy;
- }
- void ScaryBot::attackTarget()
- {
- // All units attack the target.
- set<Unit *> units = Broodwar->self()->getUnits();
- for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
- {
- (*unit)->attackUnit(target);
- }
- }
- void ScaryBot::moveToPos(set<Unit *> units, Position position)
- {
- for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
- {
- (*unit)->move(position);
- }
- }
- //void ScaryBot::moveToCentre
- void ScaryBot::drawMarkings()
- {
- if (target != NULL && target->exists())
- {
- // Mark target on map.
- Position p = target->getPosition();
- Broodwar->drawBoxMap(p.x() - 2, p.y() - 2, p.x() + 2, p.y() + 2, Colors::Red, true);
- }
- }
- // Callback stubs
- void ScaryBot::onEnd(bool isWinner) {}
- void ScaryBot::onNukeDetect(Position target) {}
- void ScaryBot::onPlayerLeft(Player* player) {}
- void ScaryBot::onReceiveText(Player* player, string text) {}
- void ScaryBot::onSaveGame(string gameName) {}
- void ScaryBot::onSendText(string text) {}
- void ScaryBot::onUnitCreate(Unit* unit) {}
- void ScaryBot::onUnitDestroy(Unit* unit) {}
- void ScaryBot::onUnitDiscover(Unit* discoveredUnit) {}
- void ScaryBot::onUnitEvade(Unit* unit) {}
- void ScaryBot::onUnitHide(Unit* unit) {}
- void ScaryBot::onUnitMorph(Unit* unit) {}
- void ScaryBot::onUnitRenegade(Unit* unit) {}
- void ScaryBot::onUnitShow(Unit* unit) {}
Add Comment
Please, Sign In to add comment