#include "stdafx.h"
#include "UpdateUnitRecordsProcess.h"
#include "../PerceptualState.h"
#include "../UnitRecord/UnitRecord.h"

static const size_t UPDATE_FREQUENCY = 5;

UpdateUnitRecordsProcess::UpdateUnitRecordsProcess(const Process* parent, Map* map, PathPrediction& pathPrediction)
    : PeriodicProcess(parent, Scheduler::SCHEDULING_MEDIUM_PRIORITY, UPDATE_FREQUENCY, 0)
    , m_map(map)
    , m_pathPrediction(pathPrediction)
{}

void UpdateUnitRecordsProcess::run()
{
  // Update the map
  m_map->updateOwnUnits();
  m_map->updateEnemyUnits();
  m_map->updateNeutralUnits();

  // Log and predict units' position
  const std::set< BWAPI::Unit* >& units = BWAPI::Broodwar->enemy()->getUnits();
  for(std::set< BWAPI::Unit* >::const_iterator itt = units.begin(); itt != units.end(); ++itt)
  {
    UnitRecord* unit = Spar->getPerceptualState()->getUnitRecord(*itt);
    unit->recordCurrentPosition();

    UnitRecord::PredictedPaths positions = m_pathPrediction.predictFuturePaths(unit, UPDATE_FREQUENCY + NB_NEXT_FRAMES_THREAT);
    unit->setPredictedPaths(positions);
  }
}
