BWAPI
SPAR/AIModule/SparAIModule/PerceptualState/InfluenceMap/StructuralImportance.h
Go to the documentation of this file.
00001 #pragma once
00002 #include "InfluenceMap.h"
00003 #include <BWAPI.h>
00004 #include <cassert>
00005 
00006 struct StructuralImportance
00007 {
00008   int value;
00009 
00010   StructuralImportance() { reset(); }
00011   void reset() { value = 0; }
00012 
00013   bool isNull() const { return value <= 0; }
00014 
00015   void display(int x, int y, int w, int h, BWAPI::Player* player) const
00016   {
00017     unused(player);
00018     if (value > 0)
00019     {
00020       BWAPI::Color colour(0, 0, std::min(static_cast<int>(value / 500.f), 0xff));
00021       BWAPI::Broodwar->drawEllipseMap(x + w/2, y + h/2, w/2, h/2, colour, false);
00022     }
00023   }
00024 
00025   static void update(InfluenceMap<StructuralImportance>& map)
00026   {
00027     assert(map.getWidth()  == BWAPI::Broodwar->mapWidth()
00028         && map.getHeight() == BWAPI::Broodwar->mapHeight());
00029 
00030     map.reset();
00031 
00032     const std::set<BWAPI::Unit*>& units = map.getPlayer()->getUnits();
00033     for(std::set<BWAPI::Unit*>::const_iterator i = units.begin(); i != units.end(); ++i)
00034     {
00035       BWAPI::Unit* unit = *i;
00036       BWAPI::UnitType type = unit->getType();
00037       const BWAPI::Position& position = unit->getPosition();
00038       if (type.isBuilding())
00039       {
00040         // Lifted from BWAPI source code (GameImpl.cpp)
00041         int startX = (position.x() - type.dimensionLeft()) / TILE_SIZE;
00042         int endX   = (position.x() + type.dimensionRight() + TILE_SIZE - 1) / TILE_SIZE; // Division - round up
00043         int startY = (position.y() - type.dimensionUp()) / TILE_SIZE;
00044         int endY   = (position.y() + type.dimensionDown() + TILE_SIZE - 1) / TILE_SIZE;
00045 
00046         // Update the influence of all tiles the building is on
00047         for (int y = startY; y < endY; y++)
00048         {
00049           for (int x = startX; x < endX; x++)
00050           {
00051             StructuralImportance influence = map.getInfluence(x, y);
00052             influence.value += type.buildScore();
00053             map.setInfluence(x, y, influence);
00054           }
00055         }
00056       }
00057     }
00058   }
00059 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines