]> ruin.nu Git - hbs.git/blobdiff - bs/planet.cpp
Fixed a bug in the shipstealing code..
[hbs.git] / bs / planet.cpp
index e6fbe51c006ccb0e87f773b8b5112be6f0a60e08..4133277d62d7cc080abfee462df05db499a9756e 100644 (file)
@@ -23,6 +23,7 @@ using namespace std;
 Planet::Planet()
 {
        m_sRace = "Planet";
+       m_iStays = -1;
 }
 
 Planet::~Planet(){
@@ -131,6 +132,9 @@ void Planet::takeRoids(std::string type, int number, int tick = 0)
        for (int i = ticks; i <= tick; ++i )
                m_Roids[type].push_back(roids);
        m_Roids[type][tick] -= number;
+
+       if (type != "uninit")
+               addPlanetScore(-3000*number, tick);
 }
 
 
@@ -143,6 +147,13 @@ void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile
                        return;
 
        int skipped = 0;
+
+       for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
+               (*i)->resetTicks();
+
+       for (vector<Fleet*>::iterator i = hostile.begin(); i != hostile.end(); ++i)
+               (*i)->resetTicks();
+
        for(int tick = 1; skipped < 20; ++tick)
        {
                //See who's in the battle at the current tick
@@ -174,7 +185,11 @@ void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile
 
                //Reset roids
                for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
+               {
                        setRoids(roid->first, roids(roid->first, tick-1), tick);
+               }
+
+               setPlanetScore(planetScore(tick - 1), tick);
                
                
                calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
@@ -182,8 +197,11 @@ void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile
                //allFriends.printFleet();
 
                allFriends.distributeLossesGains(friends, tick);
+               allFriends.distributeStolenShips(stealfriendly, friends, tick);
+
                allHostiles.distributeLossesGains(hostiles, tick);
                allHostiles.distributeCappedRoids(hostiles, tick);
+               allHostiles.distributeStolenShips(stealhostile, hostiles, tick);
 
                calculateScoreLoss(friends, tick);
 
@@ -191,6 +209,9 @@ void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile
                        setRoids(i->first, roids(i->first, 1), tick);*/
        }
 
+       for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
+               (*i)->calculateSalvage();
+
        for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
        {
                for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); ++j)
@@ -200,8 +221,17 @@ void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string, std::map<std::string, int> >& stealfriendly, std::map<std::string, std::map<std::string, int> >&  stealhostile, int tick = 0)
+void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string, std::map<std::string, int> >& stealfriendly, std::map<std::string, std::map<std::string, int> >&  stealhostile, int tick = 1)
 {
+       if (planetScore(tick - 1) > 0)
+               setCapping(float(planetScore(tick - 1)) / hostile->score() /  10, tick);
+       else
+               setCapping(0, tick);
+                       
+       map<string, int> pods;
+
+
+       //FIXME: Need to change this and allow multiple shiptypes with the same initiative.
        map<int, string> unitsinit; // order units after their ininitiative
        for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
                unitsinit[i->second.initiative()] = i->first;
@@ -224,6 +254,9 @@ void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string,
                {
                        hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
                        friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
+
+                       friendlytemp->calculateLostStealships(unittype, stealfriendly[unittype], 1);
+                       hostiletemp->calculateLostStealships(unittype, stealhostile[unittype], 1);
                }
                else
                {
@@ -234,40 +267,35 @@ void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string,
 
                if (s_Units[unittype].type() == "Pod")
                {
-                       if (planetScore(tick - 1) > 0)
-                       {
-                               float capping = float(planetScore(tick - 1)) / hostile->score() /  10;
-                       
-                               cerr << "Capping is: " << capping << endl;
+                       //FIXME: Prolly need to recode the whole capping section for r7, due to multiple pods thingy
+                       cerr << "Capping is: " << capping(tick) << endl;
        
-                               if (capping > 0.15)
-                                       capping = 0.15;
-                               if (capping > 0)
+                       if (capping(tick) > 0)
+                       {
+                               for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
                                {
-                                       for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid)
-                                       {
-                                               int caproids = capping * roids(roid->first, tick - 1);
-                                               int freepods = hostiletemp->freeFleet(unittype, 1);
+                                       int caproids = capping(tick) * roids(roid->first, tick - 1);
+                                       int freepods = hostiletemp->freeFleet(unittype, 1);
                
-                                               cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
-                                               cerr << freepods << " free pods available\n";
-                                               
-                                               if (freepods <= 0)
-                                                       break;
-                                               if (freepods < caproids)
-                                                       caproids = freepods;
+                                       cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n";
+                                       cerr << freepods << " free pods available\n";
+                               
+                                       if (freepods <= 0)
+                                               break;
+                                       if (freepods < caproids)
+                                               caproids = freepods;
                
-                                               cerr << caproids << " roids stolen\n";
-                                                       
-                                               takeRoids(roid->first, caproids, tick);
+                                       cerr << caproids << " roids stolen\n";
+
+                                       takeRoids(roid->first, caproids, tick);
                
-                                               //FIXME: Going to move this to the distribute roids section instead.. Not really move, I'll keep this, but "regenerate" the pods in the distribute ships function.
-                                               hostiletemp->killFleet(unittype, caproids, 1);
-                                               //int totroids = caproids + hostiletemp->resource(roid->first, 0);
-                                               hostiletemp->addResource(roid->first, caproids, 1);
+                                       //FIXME: Going to move this to the distribute roids section instead.. Not really move, I'll keep this, but "regenerate" the pods in the distribute ships function.
+                                       hostiletemp->killFleet(unittype, caproids, 1);
+                                       pods[unittype] += caproids;
+                                       //int totroids = caproids + hostiletemp->resource(roid->first, 0);
+                                       hostiletemp->addResource(roid->first, caproids, 1);
                                                
-                                               cerr << caproids << " stolen " << roid->first << " roids\n";
-                                       }
+                                       cerr << caproids << " stolen " << roid->first << " roids\n";
                                }
                        }
                }
@@ -278,8 +306,12 @@ void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string,
                *hostile = *hostiletemp;
                delete hostiletemp;
        }
+       hostile->addFleet(pods, 1);
+       
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
 void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick = 1)
 {
        for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
@@ -292,3 +324,35 @@ void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick = 1)
        }
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
+float Planet::capping(int tick = 0)
+{
+       int ticks = m_Capping.size();
+
+       --ticks;
+
+       if (ticks < tick)
+               return 0;
+
+       return m_Capping[tick];
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Planet::setCapping(float capping, int tick = 0)
+{
+       int ticks = m_Capping.size();
+
+       for (int i = ticks; i <= tick; ++i)
+       {
+               m_Capping.push_back(0);
+       }
+
+       if (capping <= 0.15 && capping >= 0)
+               m_Capping[tick] = capping;
+       else if (capping >= 0.15)
+               m_Capping[tick] = 0.15;
+       else
+               m_Capping[tick] = 0;
+}