]> ruin.nu Git - hbs.git/blobdiff - bs/fleet.cpp
takeShoot function might work now..
[hbs.git] / bs / fleet.cpp
index 4989573d87f069b3e3bd6e27c4b009e9a0e7268c..432f16595efadab7d1e8c479fa8cb03678ac7292 100644 (file)
 
 #include "fleet.h"
 
+#include <iostream>
+#include <cstdlib>
 using namespace std;
 
 //Static variables
 map<string, vector<int> > Fleet::s_Races;
-map<string, UnitType > Fleet::s_Units;
+UnitList Fleet::s_Units;
 
 Fleet::Fleet()
 {
+       m_iETA = 0;
+       m_sRace = "Cathaar";
 }
+
 Fleet::~Fleet(){
 }
 
@@ -38,7 +43,7 @@ void Fleet::setName(string sName)
 
 //////////////////////////////////////////////////////////////////////////
 //
-string Fleet::Name()
+string Fleet::name() const
 {
        return m_sName;
 }
@@ -49,7 +54,7 @@ string Fleet::Name()
  * s_Races and checks if it finds the race it returns true, if it reaches
  * the end without finding it it returns false.
  */
-bool Fleet::setRace(std::string sRace)
+bool Fleet::setRace(string sRace)
 {
        m_sRace = sRace;
        for (map<string, vector<int> >::iterator i = s_Races.begin(); i != s_Races.end(); i++)
@@ -62,7 +67,7 @@ bool Fleet::setRace(std::string sRace)
 
 //////////////////////////////////////////////////////////////////////////
 //
-string Fleet::Race()
+string Fleet::race() const
 {
        return m_sRace;
 }
@@ -72,15 +77,316 @@ string Fleet::Race()
 /** This function iterates through m_Fleet and adds all numbers together to
  * produce a total.
  */
-int Fleet::NumberOfShips()
+int Fleet::numberOfShips() const
 {
        int total = 0;
 
-       for (map<string, vector<int> >::iterator i = m_Fleet.begin(); i != m_Fleet.end(); i++)
+       for (map<string, vector<int> >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
        {
-               total += m_Fleet[(*i).first][0];
+               if (i->second.size() != 0)
+                       total += i->second[0];
        }
 
        return total;
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setETA(int eta)
+{
+       m_iETA = eta;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+int  Fleet::ETA() const
+{
+       return m_iETA;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setRaces(map<string, vector<int> >& races)
+{
+       s_Races = races;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setUnits(UnitList& units)
+{
+       s_Units = units;
+
+       /*
+
+       for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
+       {
+               cerr << s_Units[(*i).first].Name() << "\t\t"
+                       << s_Units[(*i).first].race() <<"\t"
+                       << s_Units[(*i).first].unitClass() << "\t"
+                       << s_Units[(*i).first].target(0) << "\t"
+                       << s_Units[(*i).first].target(1) << "\t"
+                       << s_Units[(*i).first].target(2) << "\t"
+                       << s_Units[(*i).first].initiative() << "\t"
+                       << s_Units[(*i).first].agility() << "\t"
+                       << s_Units[(*i).first].weaponSpeed() << "\t"
+                       << s_Units[(*i).first].guns() << "\t"
+                       << s_Units[(*i).first].power() << "\t"
+                       << s_Units[(*i).first].armor() << "\t"
+                       << s_Units[(*i).first].EMP() << "\t"
+                       << s_Units[(*i).first].totRes() << "\t"
+                       << s_Units[(*i).first].fuel() << "\t"
+                       << s_Units[(*i).first].ETA() << "\t"
+                       << s_Units[(*i).first].type() << endl;
+       }
+       */
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+const map<string, vector<int> >& Fleet::Races()
+{
+       return s_Races;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+const UnitList& Fleet::Units()
+{
+       return s_Units;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+vector<int> Fleet::RacesAllowed() const
+{
+       return s_Races[m_sRace];
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+unsigned Fleet::score(int tick = 0) const
+{
+       unsigned tot_score = 0;
+
+       for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
+       {
+                 if (i->second.size() >= tick)
+                               break;
+                       tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
+       }
+
+       return tot_score;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setFleet(string unittype, int number, int tick = 0)
+{
+       if (m_Fleet[unittype].size() <= tick)
+       {
+               m_Fleet[unittype].push_back(number);
+               return;
+       }
+       m_Fleet[unittype][tick] = number;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+int     Fleet::fleet(string unittype, int tick = 0)
+{
+       int ticks = m_Fleet[unittype].size();
+       if (ticks == 0)
+               return 0;
+
+       --ticks;
+
+       if (ticks < tick)
+               m_Fleet[unittype][ticks];
+
+       return m_Fleet[unittype][tick];
+}
+
+//////////////////////////////////////////////////////////////////////////
+//FIXME
+void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
+{
+       for (UnitList::iterator i = s_Units.begin();  i != s_Units.end(); ++i)
+       {
+               if (m_Fleet[i->first].size() == 0)
+                               m_Fleet[i->first].push_back(0);
+
+               for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
+               {
+               // FIXTHIS!!    m_Fleet[i->first][0] += j->fleet(i->first, tick);
+               }
+       }
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
+{
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays = 0, int tick = 0)
+{
+       vector<Fleet*> fl;
+       for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
+       {
+               if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays)
+                       fl.push_back((*i));
+               else if ((*i)->name() == "Home Planet")
+                       fl.push_back((*i));
+       }
+       return fl;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+int Fleet::freeFleet(std:: string unittype, int tick = 0)
+{
+       if (m_Fleet[unittype].size() == 0)
+               return 0;
+       if (m_BlockedFleet[unittype].size() < tick)
+               return m_Fleet[unittype][tick];
+
+       return m_Fleet[unittype][tick] - m_BlockedFleet[unittype][tick];
+}
+
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
+{
+       int guns = s_Units[unittype].guns() * number;
+       int power = s_Units[unittype].power() * number;
+
+       int gunsleft = guns;
+       for (vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
+       {
+
+               while (gunsleft > 0)
+               {
+       
+                       map<string, int*> targets;
+
+                       for (UnitList::iterator j = s_Units.begin(); j != s_Units.end(); ++j)
+                       {
+                               if (m_Fleet[j->first].size() == 0)
+                                       break;
+
+                               if (m_Fleet[j->first].size() == 1)
+                                       m_Fleet[j->first].push_back(m_Fleet[j->first][0]);
+
+                               if (m_Fleet[j->first][1] > 0 && (*i) == j->second.type())
+                                       targets[j->first] = &m_Fleet[j->first][1];
+                       }
+
+                       int total = 0;
+                       for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
+                               total += (*j->second);
+
+                       for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
+                       {
+                               int maxguns = (*j->second)/total * guns;
+                               
+
+                               if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
+                                       m_Armor[j->first] = s_Units[j->first].armor();
+                               int k = maxguns;
+                               while (k > 0)   
+                               {
+
+                                       if (*(j->second) <= 0)
+                                               break;
+                                       k -= 100/(25+s_Units[unittype].weaponSpeed() - s_Units[j->first].agility());
+                                       m_Armor[j->first] -= s_Units[unittype].power();
+                                       if (m_Armor[j->first] <= 0)
+                                       {
+                                               m_Armor[j->first] = s_Units[j->first].armor();
+                                               (*j->second)--;
+                                               
+                                               //There is a chance that we're hitting a blocked ship.
+                                               if (m_BlockedFleet[j->first].size() >= 1)
+                                               {
+                                                       int test = rand() % m_BlockedFleet[j->first][0];
+                                                       if (test == 1
+                                                               && m_BlockedFleet[j->first][0] > 0)
+                                                       {
+                                                               if (m_BlockedFleet[j->first].size() == 1)
+                                                                       m_BlockedFleet[j->first].push_back(m_BlockedFleet[j->first][0] - 1);
+                                                               else if (m_BlockedFleet[j->first][1] > 0)
+                                                                       m_BlockedFleet[j->first][1]--;
+                                                       }
+                                               }
+                                       }
+                               }
+                               if (k <= 0)
+                                       gunsleft -= maxguns;
+                               else
+                                       gunsleft -= maxguns - k;
+                       }
+               }
+       }
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::takeEMP(std::string unittype, int number)
+{
+
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::killFleet(std::string unittype, int number, int tick = 0)
+{
+       if (m_Fleet[unittype].size() <= tick)
+       {
+               m_Fleet[unittype].push_back(m_Fleet[unittype][m_Fleet[unittype].size()] - number);
+               return;
+       }
+       m_Fleet[unittype][tick] -= number;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setResource(std::string type, int number, int tick = 0)
+{
+
+       if (m_Resources[type].size() <= tick)
+               m_Resources[type].push_back(number);
+       m_Resources[type][tick] = number;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+int Fleet::resource(std::string type, int tick = 0) const
+{
+       vector<int>const* resource = 0;
+       for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
+       {
+               if (i->first == type)
+               {
+                       resource = &i->second;
+                       break;
+               }
+       }
+       if (resource == 0)
+               return 0;
+
+       int ticks = resource->size();
+
+       if( ticks == 0)
+               return 0;
+
+       --ticks;
+
+       if (ticks < tick)
+               return resource->at(ticks);
+       return resource->at(tick);
+}
+