BWAPI
SPAR/AIModule/SparAIModule/PerceptualState/InfluenceMap/Threat.h
Go to the documentation of this file.
00001 #pragma once
00002 #include "InfluenceMap.h"
00003 #include "../../Utils/BresenhamCircle.h"
00004 #include <BWAPI.h>
00005 #include <cassert>
00006 #include <functional>
00007 #include <set>
00008 
00012 struct Threat
00013 {
00014   int ground;
00015   int air;
00016 
00017   Threat() { reset(); }
00018   void reset() { ground = air = 0; }
00019 
00020   bool isNull() const
00021   {
00022     return ground == 0 && air == 0;
00023   }
00024 
00025   void display(int x, int y, int w, int h, BWAPI::Player* player) const
00026   {
00027     if (ground > 0)
00028     {
00029       int val = std::min<int>(ground + 50, 0xff);
00030 
00031       BWAPI::Color colour(val, 0, 0);
00032       if (player == BWAPI::Broodwar->self())
00033       {
00034         colour = BWAPI::Color(0, val, 0);
00035         x++;
00036         y++;
00037         w-=2;
00038         h-=2;
00039       }
00040 
00041       BWAPI::Broodwar->drawBox(BWAPI::CoordinateType::Map, x, y, x+w, y+h, colour, false);
00042     }
00043   }
00044 
00045   static void update(InfluenceMap<Threat>& map);
00046 
00047 private:
00048   template <class Iterator>
00049   static void update(InfluenceMap<Threat>& map, Iterator begin, Iterator end)
00050   {
00051     for (; begin != end; ++begin)
00052     {
00053       update(map, *begin);
00054     }
00055   }
00056 
00057   static void update(InfluenceMap<Threat>& map, BWAPI::Unit* unit);
00058   static void update(InfluenceMap<Threat>& map, const EnemyUnit* unit);
00059 
00060   static void updateInfluence(InfluenceMap<Threat>& map, BWAPI::Position position, int groundRange, int groundDamage, int airRange, int airDamage, double topSpeed)
00061   {  
00062     // Top speed in pixel per frame: zealot = 4; for dragoon = 5
00063     int speedFactor = static_cast<int>(topSpeed/2.0 + 0.5);
00064 
00065     // Update the influence of all tiles within range
00066     if (groundDamage > 0)
00067         {
00068                 BresenhamCircle(Functor(map, position, (groundDamage % 2 ? groundDamage/2 : groundDamage/2 +1), 0), groundRange, true);
00069                 BresenhamCircle(Functor(map, position, groundDamage/2, 0), groundRange + speedFactor, true);
00070         }
00071     if (airDamage > 0)
00072         {
00073       BresenhamCircle(Functor(map, position, 0, (airDamage % 2 ? airDamage/2 : airDamage/2 +1)),    airRange,    true);
00074       BresenhamCircle(Functor(map, position, 0, airDamage/2), airRange + speedFactor,    true);
00075         }
00076   }
00077 
00082   struct Functor : std::binary_function<int,int,void>
00083   {
00091     Functor(InfluenceMap<Threat>& map, const BWAPI::Position& position, int groundDamage, int airDamage)
00092       : m_map(map)
00093       , m_x(position.x() / TILE_SIZE), m_y(position.y() / TILE_SIZE)
00094       , m_ground(groundDamage)
00095       , m_air(airDamage)
00096     {
00097       // FIXME
00098       assert(map.getWidth()  == BWAPI::Broodwar->mapWidth()
00099           && map.getHeight() == BWAPI::Broodwar->mapHeight());
00100     }
00101 
00109     result_type operator()(first_argument_type x, second_argument_type y);
00110 
00111   private:
00112     InfluenceMap<Threat>& m_map;
00113     int m_x, m_y;
00114     int m_ground, m_air;
00115   };
00116 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines