/* LICENSE:
  =========================================================================
    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
    School of Computer Science, Carnegie Mellon University
    All rights reserved.
  ========================================================================= */

#include <math.h>
#include "../headers/Dictionary.h"
#include "../headers/MessageStructures.hh"
#include "../headers/SPOutEncoder.h"
#include "../Vision/VisionInterface.h"
#include "../Motion/MotionInterface.h"
#include "../Localization/GLocalization.h"
#include "../headers/Geometry.h"
#include "../headers/Util.h"
#include "Behaviors.h"
#include "../headers/Config.h"
#include "../headers/SPInBufferIDs.h"

#include "Attacker.h"
#include "BeLowLevel.h"
#include "FeatureSet.h"

Attacker *behAttacker = NULL;

void BehaviorSystem::new_attack(FeatureSet *myFeatures) 
{
  logCallChain(CALL_test);

  if(behAttacker==NULL)
    behAttacker = new Attacker();
  
  // Run the attacker
  (*behAttacker)(myFeatures, command);

  static int count=0;
  if(config.spoutConfig.dumpBehaviorNames!=0){
    count = (count + 1) % config.spoutConfig.dumpBehaviorNames;
    
    if(count == 0){
      static const int buf_size=32*1024;
      static uchar buf[buf_size];
      uchar *bufp;

      if(behavior_names_stream!=NULL){
        bufp = buf;
        bufp = behAttacker->encodeAllNames(myFeatures->current_time,bufp,buf_size);
        behavior_names_stream->writeBinary(buf,bufp-buf,myFeatures->current_time);
      }
    }
    
  }

  static int count2=0;
  if(config.spoutConfig.dumpBehaviorTrace!=0){
    count2 = (count2 + 1) % config.spoutConfig.dumpBehaviorTrace;
    if(count2 == 0){
      static const int buf_size=1024;
      static uchar buf[buf_size];
      uchar *bufp;

      if(behavior_trace_stream!=NULL){
        bufp = buf;
        bufp = behAttacker->encodeTrace(bufp,buf_size);
        behavior_trace_stream->writeBinary(buf,bufp-buf,myFeatures->current_time);
      }
    }
  }
}
