]> ruin.nu Git - hbs.git/blobdiff - bs/fleet.cpp
Started the work to split up the pods better..
[hbs.git] / bs / fleet.cpp
index b4e4335a4c71486f7db35b461765b8ba31566f16..13bafb7aa071ace95640bb0e84dca2840aeb3cef 100644 (file)
@@ -139,8 +139,7 @@ void Fleet::setUnits(UnitList& units)
                        << s_Units[(*i).first].ETA() << "\t"
                        << s_Units[(*i).first].type() << endl;
        }
-       
-       */
+*/     
 }
 
 //////////////////////////////////////////////////////////////////////////
@@ -172,8 +171,13 @@ unsigned Fleet::score(int tick = 0) const
 
        for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i)
        {
-                 if (i->second.size() >= tick)
-                               break;
+               int ticks =     i->second.size();
+               if (ticks == 0)
+                       continue;
+               --ticks;
+               if ( ticks < tick)
+                       tot_score += i->second[ticks] * s_Units[i->first].totRes() / 10;
+               else
                        tot_score += i->second[tick] * s_Units[i->first].totRes() / 10;
        }
 
@@ -213,6 +217,38 @@ int         Fleet::fleet(string unittype, int tick = 0)
        return m_Fleet[unittype][tick];
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
+int Fleet::blockedFleet(std::string unittype, int tick = 0)
+{
+       int ticks = m_BlockedFleet[unittype].size();
+       if (ticks == 0)
+               return 0;
+
+       --ticks;
+
+       if (ticks < tick)
+               return 0;
+
+       return m_BlockedFleet[unittype][tick];
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setBlockedFleet(std::string unittype, int number, int tick = 0)
+{
+       int ticks = m_BlockedFleet[unittype].size();
+
+       for (int i = ticks; i <= tick; ++i)
+       {
+               m_BlockedFleet[unittype].push_back(0);
+       }
+       m_BlockedFleet[unittype][tick] = number;
+
+       cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl;
+
+}
+
 //////////////////////////////////////////////////////////////////////////
 //FIXME
 void Fleet::addToThis(std::vector<Fleet*> fleets, int tick = 0)
@@ -259,8 +295,15 @@ void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
                        int fl1 = (*j)->fleet(unittype, tick - 1);
                        float part = float(fl1) / fleet(unittype, 0) ;
                        int lost =  totallost * part;
-                       cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
                        (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
+
+                       cerr << (*j)->name() << " gaining " << lost << " " << unittype  << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." <<  endl;
+
+                       lost = part * blockedFleet(unittype, 0);
+
+                       cerr << (*j)->name() << " got " << lost << " blocked " << unittype  << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; 
+
+                       (*j)->setBlockedFleet(unittype, lost, tick);
                }
        }
 }
@@ -331,7 +374,8 @@ void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, in
                                if (m_Fleet[j->first].size() == 0)
                                        continue;
 
-                               if (m_Fleet[j->first].size() == 1)
+
+                               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;
@@ -435,6 +479,9 @@ void Fleet::takeEMP(std::string unittype, int number)
                                if (j->second.type() == "PDS")
                                        continue;
 
+                               if (freeFleet(j->first, 1) <= 0)
+                                       continue;
+
                                if (m_Fleet[j->first].size() == 0)
                                        continue;
 
@@ -461,7 +508,7 @@ void Fleet::takeEMP(std::string unittype, int number)
 
                        for (map<string, int*>::iterator j = targets.begin(); j != targets.end(); ++j)
                        {
-                               int maxguns = (*j->second)/total * guns;
+                               float maxguns = float((*j->second))/total * guns;
                                cerr << "Now shooting at target: " << j->first << endl;
 
                                double k = maxguns;
@@ -471,7 +518,7 @@ void Fleet::takeEMP(std::string unittype, int number)
                                while (k > 0)   
                                {
 
-                                       if (*(j->second) <= 0)
+                                       if (*(j->second) <= blockedFleet(j->first, 1))
                                                break;
 
                                        int eres = s_Units[j->first].EMP();
@@ -513,11 +560,28 @@ void Fleet::killFleet(std::string unittype, int number, int tick = 0)
 void Fleet::setResource(std::string type, int number, int tick = 0)
 {
 
-       if (m_Resources[type].size() <= tick)
+       int ticks = m_Resources[type].size();
+       for (int i = ticks; i <= tick; ++i)
                m_Resources[type].push_back(number);
        m_Resources[type][tick] = number;
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::addResource(std::string type, int number, int tick = 0)
+{
+
+       int ticks = m_Resources[type].size();
+       int latest = 0;
+
+       if (ticks > 0)
+               latest = m_Resources[type][ticks - 1];
+
+       for (int i = ticks; i <= tick; ++i)
+               m_Resources[type].push_back(latest);
+       m_Resources[type][tick] += number;
+}
+
 //////////////////////////////////////////////////////////////////////////
 //
 int Fleet::resource(std::string type, int tick = 0) const
@@ -581,3 +645,50 @@ void Fleet::blockFleet(std::string unittype, int number, int tick = 0)
                m_BlockedFleet[unittype].push_back(number);
        }
 }
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick = 0)
+{
+       for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
+       {
+               string res = i->first;
+
+               cerr << "Distributing type: " << res << endl;
+               for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
+                       cout << (*j) << endl;
+
+               if (m_Resources[res].size() < 2)
+                       continue;
+               if (m_Resources[res][1] == 0)
+                       continue;
+
+                
+               int totcapped = resource(res,1) - resource(res, 0);
+
+
+               cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl;
+
+               cerr << "Total number of roids before: " << resource(res, 0)  << " and after : " << resource(res, 1) << endl;
+               
+               for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
+               {
+                       unsigned fl1 = (*j)->score(tick - 1);
+                       float part = float(fl1) / score(0) ;
+                       int lost =  totcapped * part;
+
+                       cerr << (*j)->name() << " gaining " << lost << " " << res  << " since it's " << part * 100 << "% of the whole score, and it had : " << fl1 << " score last tick.. compared to fleet total of: " << score(0) <<  endl;
+                       (*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick);
+               }
+       }
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::addPodsForLostRoids(int tick = 1)
+{
+       for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
+       {
+               setFleet("Astro Pod", fleet("Astro Pod", tick) + (resource(i->first, tick) - resource(i->first, tick - 1)), tick);
+       }
+}