/***************************************************************************
 *   Copyright (C) 2004 by Raphael Langerhorst                             *
 *   raphael-langerhorst@gmx.at                                            *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "GRadiatingAgent.h"

#include <GObject.h>
#include <GElementInfluence.h>
#include <GEnergy.h>
#include <GForm.h>

using namespace GCS;

namespace GBE {

GRadiatingAgent::GRadiatingAgent()
{
}


GRadiatingAgent::~GRadiatingAgent()
{
}


void GRadiatingAgent::run()
{
  bool has_energy,has_form;
  has_energy = requestObject()->hasEnergy();
  has_form = requestObject()->hasForm();

  Q_CHECK_PTR(has_energy);
  Q_CHECK_PTR(has_form);

  if (!has_energy)
  {
    qWarning("can't radiate influence without energy!");
    return;
  }
  
  while(!shutdown)
  {
    bool ok;
    unsigned long interval = xmlGetULongInteger("/radiation/interval",ok);
    if (!ok || interval == 0)
    {
      interval = 1000;
      initInterval(interval);
    }
    
    double fraction = xmlGetDouble("/radiation/fraction",ok);
    if (fraction < 0)
    {
      fraction=0;
      initFraction(fraction);
    }
    
    double MinEnergyAmount = xmlGetDouble("/radiation/minenergy",ok);
    if (MinEnergyAmount < 0)
    {
      MinEnergyAmount = 0;
      initMinEnergy(MinEnergyAmount);
    }
    
    double MaxEnergyAmount = xmlGetDouble("/radiation/maxenergy",ok);
    if (MaxEnergyAmount < MinEnergyAmount)
    {
      MaxEnergyAmount = MinEnergyAmount;
      initMaxEnergy(MaxEnergyAmount);
    }
    
    GEnergy* e = requestEnergy();
    if (e->amount() > MinEnergyAmount && e->amount() > 0 && fraction > 0)
    {
      
      if (e->amount() > MaxEnergyAmount && (e->amount() - MaxEnergyAmount) > fraction*e->amount())
      {
        fraction = (e->amount() - MaxEnergyAmount)/e->amount();
        qDebug(QString("fraction exceeded maximum, temporary fraction: %1").arg(QString::number(fraction)));
      }
      
      if ((e->amount() - MinEnergyAmount) < fraction*e->amount())
      {
        fraction = (e->amount() - MinEnergyAmount)/e->amount();
        qDebug(QString("fraction below minimum, temporary fraction: %1").arg(QString::number(fraction)));
      }
      
//       qDebug(QString("RADIATION FRACTION: %1").arg(QString::number(fraction)));
      GElementInfluence influence( getElementID(), e->take(fraction));
      emit radiateInfluence( influence );
      emit energyChanged(*requestEnergy());
//       qDebug(QString("RADIATION DONE"));
    }
    msleep(interval);
  }
}


/*
<radiation>
<fraction>double value</fraction>
<minenergy>double value</minenergy>
<maxenergy>double value</maxenergy>
<interval>unsigned long integer</interval> <!-- milliseconds -->
</radiation>
*/

void GRadiatingAgent::initFraction(double radiation_fraction)
{
  bool ok;
  xmlSetDouble("/radiation/fraction",radiation_fraction,ok);
}

void GRadiatingAgent::initMinEnergy(double min_energy_amount)
{
  bool ok;
  xmlSetDouble("/radiation/minenergy",min_energy_amount,ok);
}

void GRadiatingAgent::initMaxEnergy(double max_energy_amount)
{
  bool ok;
  xmlSetDouble("/radiation/maxenergy",max_energy_amount,ok);
}

void GRadiatingAgent::initInterval(unsigned long radiate_interval)
{
  bool ok;
  xmlSetULongInteger("/radiation/interval",radiate_interval,ok);
}

};


syntax highlighted by Code2HTML, v. 0.9.1