]> ruin.nu Git - hbs.git/blobdiff - bs/fleet.cpp
solved an evil bug in Fleet::distributeLossesGains and some other stuff
[hbs.git] / bs / fleet.cpp
index 97be5aaf867286b7e24d8d178dac634eb9667396..2daa5cf052920866b68d158819a42e7a23c554d9 100644 (file)
@@ -18,6 +18,7 @@
 #include "fleet.h"
 
 #include <iostream>
+#include <cstdlib>
 using namespace std;
 
 //Static variables
@@ -116,8 +117,8 @@ 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"
@@ -138,6 +139,7 @@ void Fleet::setUnits(UnitList& units)
                        << s_Units[(*i).first].ETA() << "\t"
                        << s_Units[(*i).first].type() << endl;
        }
+       
        */
 }
 
@@ -180,28 +182,39 @@ unsigned Fleet::score(int tick = 0) const
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Fleet::setFleet(string unittype, int number)
+void Fleet::setFleet(string unittype, int number, int tick = 0)
 {
-       if (m_Fleet[unittype].size() == 0)
+       int earlier = 0;
+       int ticks = m_Fleet[unittype].size();
+
+       if (ticks != 0)
+               earlier = m_Fleet[unittype][ticks - 1];
+
+       for (int i = ticks; i <= tick; ++i)
        {
-               m_Fleet[unittype].push_back(number);
-               return;
+               m_Fleet[unittype].push_back(earlier);
        }
-       m_Fleet[unittype][0] = number;
+       m_Fleet[unittype][tick] = number;
 }
 
 //////////////////////////////////////////////////////////////////////////
 //
 int     Fleet::fleet(string unittype, int tick = 0)
 {
-       if (m_Fleet[unittype].size() == 0)
+       int ticks = m_Fleet[unittype].size();
+       if (ticks == 0)
                return 0;
 
+       --ticks;
+
+       if (ticks < tick)
+               return 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)
@@ -211,14 +224,246 @@ void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
 
                for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
                {
-               // FIXTHIS!!    m_Fleet[i->first][0] += j->fleet(i->first, tick);
+                       m_Fleet[i->first][0] += (*j)->fleet(i->first, tick);
+               }
+       }
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
+{
+       for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
+       {
+               string unittype = i->first;
+
+               cerr << "Distributing type: " << unittype << endl;
+
+               if (m_Fleet[unittype].size() < 1)
+                       continue;
+               if (m_Fleet[unittype][0] == 0)
+                       continue;
+
+               int totallost = m_Fleet[unittype][1] - m_Fleet[unittype][0];
+
+               
+               for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
+               {
+                       int lost =  totallost * ( (*j)->fleet(unittype, tick - 1) / m_Fleet[unittype][0] );
+                       cerr << (*j)->name() << " gaining " << lost << " " << unittype << endl;
+                       (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
+               }
+       }
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+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));
+                       cerr << "Using fleet " << (*i)->name() << " for tick " << tick;
+               }
+               else if ((*i)->name() == "Home Planet")
+                       fl.push_back((*i));
+       }
+       return fl;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+int Fleet::freeFleet(std:: string unittype, int tick = 0)
+{
+       int bticks = m_BlockedFleet[unittype].size();
+
+       --bticks;
+
+       if (bticks < tick)
+               return fleet(unittype, tick);
+
+
+       int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
+       if (free < 0)
+               return 0;
+       return free;
+}
+
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
+{
+
+       cerr << unittype << ":  " << number << " in number\n";
+       int guns = s_Units[unittype].guns() * number;
+       if (guns == 0)
+               return;
+
+       cerr << unittype << " with " << guns << " guns\n";
+
+       int gunsleft = guns;
+       for (int count = 0; count < 3; ++count)//vector<string>::iterator i = s_Units[unittype].target().begin(); i != s_Units[unittype].target().end(); ++i)
+       {
+               string ta = s_Units[unittype].target(count);
+    cerr << "Shooting at target class: " << ta << endl;
+               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]);
+
+                               //cerr << "Target is class: " << j->second.type() << endl;
+
+                               if (m_Fleet[j->first][1] > 0  && ( ta == j->second.unitClass() || ta == "All"))
+                               {
+
+                                       cerr << "Looking at target: " << j->first << endl;
+                                       targets[j->first] = &m_Fleet[j->first][1];
+                               }
+
+                       }
+
+                       if (targets.size() == 0)
+                               break;
+
+                       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;
+                               cerr << "Now shooting at target: " << j->first << endl;
+
+                               if (m_Armor[j->first] <= 0 || m_Armor[j->first] > s_Units[j->first].armor())
+                                       m_Armor[j->first] = s_Units[j->first].armor();
+                               float k = maxguns;
+
+                               int blaha = 0;
+                               cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
+                               cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
+                               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)--;
+                                               hitunits[j->first]++;
+                                               
+                                               //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]--;
+                                                       }
+                                               }
+                                       }
+
+                               }
+
+                               cerr << hitunits[j->first] << " units of type: " << j->first << "killed\n";
+                               if (k <= 0)
+                                       gunsleft -= maxguns;
+                               else
+                                       gunsleft -= maxguns - k;
+                       }
                }
        }
 }
 
 //////////////////////////////////////////////////////////////////////////
 //
-void distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
+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);
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::printFleet()
+{
+       for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
+       {
+               for (int tick = 0; tick < 5 ;++tick)
+               {
+                       int num = fleet(i->first, tick);
+
+                       if (num <= 0)
+                               break;
+                       cerr << num << " " << i->first << " during tick: " << tick << endl;
+               }
+       }
+}