Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. #include "ScaryBot.h"
  2. #include <string>
  3. #include <sstream>
  4. #include <iostream>
  5. using namespace std;
  6. using namespace BWAPI;
  7.  
  8. void ScaryBot::onStart()
  9. {
  10. Broodwar->printf("ScaryBot");
  11.  
  12. Broodwar->setLocalSpeed(FB_LOCAL_SPEED);
  13.  
  14.  
  15.  
  16. // Find the centre of the map.
  17. mapCentre = Position(Broodwar->mapWidth() * TILE_SIZE / 2,
  18. Broodwar->mapHeight() * TILE_SIZE / 2);
  19.  
  20. //pointOne = Position(Broodwar->mapWidth() * TILE_SIZE / 2,0);
  21. halfWidth = (Broodwar->mapWidth() * TILE_SIZE) / 2;
  22. halfHeight = (Broodwar->mapWidth() * TILE_SIZE) / 2;
  23.  
  24. halfwayCentre = Position(halfWidth /2,halfHeight);
  25.  
  26. pointTwo = Position(halfWidth/2,
  27. halfHeight + (TILE_SIZE * 5));
  28.  
  29. refHeight1 = halfHeight + TILE_SIZE * 11;
  30.  
  31. referencePos1 = Position(halfWidth-TILE_SIZE*10, refHeight1);
  32.  
  33.  
  34.  
  35.  
  36. // We don't have a target until we've scouted one out.
  37. target = NULL;
  38.  
  39.  
  40. // Move all units to the centre of the map.
  41. set<Unit *> units = Broodwar->self()->getUnits();
  42. unsigned int counter = 0;
  43. for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
  44. {
  45.  
  46. if(counter < (units.size()/2))
  47. {
  48. unit1.insert(*unit);
  49. }
  50. else if(counter >= (units.size()/2))
  51. {
  52. unit2.insert(*unit);
  53. }
  54. Position currentPosition = (*unit)->getPosition();
  55.  
  56.  
  57. counter++;
  58. }
  59.  
  60.  
  61. //start scouting.
  62. moveToPos(unit1, halfwayCentre);
  63. //moveToPos(unit1);
  64. moveToPos(unit2, pointTwo);
  65. makeFormation();
  66.  
  67. }
  68.  
  69. void ScaryBot::makeFormation()
  70. {
  71. set<Unit *> units = Broodwar->self()->getUnits();
  72. int tileCounter = 1;
  73. // Sort the units by distance to the target
  74.  
  75. for(set<Unit *>::iterator unit = units.begin(); unit!=units.end(); unit++)
  76. {
  77. set<Unit *> individual;
  78. individual.insert(*unit);
  79. moveToPos(individual, referencePos1);
  80. referencePos1 = Position(halfWidth-TILE_SIZE*10, refHeight1 - (TILE_SIZE*tileCounter + 40));
  81. tileCounter++;
  82. }
  83.  
  84. }
  85.  
  86.  
  87. void ScaryBot::onFrame()
  88. {
  89. /* If we haven't got a target or our target has been destroyed,
  90. * try to aquire a new target. If a new target cannot be found,
  91. * start scouting again.
  92. */
  93. if (target == NULL || !target->exists())
  94. {
  95. if (tryAquireTarget())
  96. {
  97. attackTarget();
  98. }
  99. else
  100. {
  101. //moveToCentre();
  102. //moveToPointOne(unit1);
  103. //moveToPointTwo(unit2);
  104. }
  105. }
  106.  
  107. drawMarkings();
  108. //Broodwar->drawCircleMap(halfwayCentre.x(), halfwayCentre.y(), 50, 6, true);
  109. }
  110.  
  111. /**
  112. * Target the enemy that causes our units to travel the least to
  113. * attack. If no enemies are visible, do nothing.
  114. */
  115. Unit *ScaryBot::tryAquireTarget()
  116. {
  117. // All the enemy units we can see.
  118. set<Unit *> emenies = Broodwar->enemy()->getUnits();
  119.  
  120. // If we can't see any enemies, there is no closest enemy.
  121. if (emenies.empty())
  122. {
  123. return NULL;
  124. }
  125.  
  126. // All of our units.
  127. set<Unit *> units = Broodwar->self()->getUnits();
  128.  
  129. // The closest enemy found so far.
  130. Unit *closestEnemy;
  131.  
  132. /* The shortest total distance our units will have travel to
  133. * attack an enemy unit.
  134. */
  135. double leastTotalTravelDistance = numeric_limits<double>::max();
  136.  
  137. for (set<Unit *>::iterator enemy = emenies.begin(); enemy != emenies.end(); enemy++)
  138. {
  139. // The total distance our units will have travel to attack enemy.
  140. double totalTraveldistance = 0.0;
  141.  
  142. for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
  143. {
  144. /* The distance our unit has to travel to attack 'enemy'.
  145. * If less than zero, our unit can attack from its
  146. * current position.
  147. */
  148. double travelDistance = (*unit)->getDistance(*enemy) - FB_DRAGOON_WEAPON_RANGE;
  149.  
  150. if (travelDistance > 0)
  151. {
  152. totalTraveldistance += travelDistance;
  153. }
  154. }
  155.  
  156. /* Would attacking 'enemy' cause our units to travel less than all
  157. * other enemies so far?
  158. */
  159. if (totalTraveldistance <= leastTotalTravelDistance)
  160. {
  161. closestEnemy = *enemy;
  162. leastTotalTravelDistance = totalTraveldistance;
  163. }
  164. }
  165.  
  166. return target = closestEnemy;
  167. }
  168.  
  169. void ScaryBot::attackTarget()
  170. {
  171. // All units attack the target.
  172. set<Unit *> units = Broodwar->self()->getUnits();
  173. for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
  174. {
  175. (*unit)->attackUnit(target);
  176. }
  177. }
  178.  
  179. void ScaryBot::moveToPos(set<Unit *> units, Position position)
  180. {
  181. for (set<Unit *>::iterator unit = units.begin(); unit != units.end(); unit++)
  182. {
  183. (*unit)->move(position);
  184. }
  185.  
  186. }
  187.  
  188.  
  189.  
  190.  
  191. //void ScaryBot::moveToCentre
  192.  
  193. void ScaryBot::drawMarkings()
  194. {
  195. if (target != NULL && target->exists())
  196. {
  197. // Mark target on map.
  198. Position p = target->getPosition();
  199. Broodwar->drawBoxMap(p.x() - 2, p.y() - 2, p.x() + 2, p.y() + 2, Colors::Red, true);
  200. }
  201. }
  202.  
  203. // Callback stubs
  204. void ScaryBot::onEnd(bool isWinner) {}
  205. void ScaryBot::onNukeDetect(Position target) {}
  206. void ScaryBot::onPlayerLeft(Player* player) {}
  207. void ScaryBot::onReceiveText(Player* player, string text) {}
  208. void ScaryBot::onSaveGame(string gameName) {}
  209. void ScaryBot::onSendText(string text) {}
  210. void ScaryBot::onUnitCreate(Unit* unit) {}
  211. void ScaryBot::onUnitDestroy(Unit* unit) {}
  212. void ScaryBot::onUnitDiscover(Unit* discoveredUnit) {}
  213. void ScaryBot::onUnitEvade(Unit* unit) {}
  214. void ScaryBot::onUnitHide(Unit* unit) {}
  215. void ScaryBot::onUnitMorph(Unit* unit) {}
  216. void ScaryBot::onUnitRenegade(Unit* unit) {}
  217. void ScaryBot::onUnitShow(Unit* unit) {}
Add Comment
Please, Sign In to add comment