]> ruin.nu Git - hbs.git/blobdiff - bs/planet.cpp
started to work on the battle report.. almost there =)
[hbs.git] / bs / planet.cpp
index 01b2125f5c9d428e5f9fb196887e6235dd448403..e5aba8575eaed8c659ecc58a4818ec31d2debc2c 100644 (file)
@@ -23,6 +23,7 @@ using namespace std;
 Planet::Planet()
 {
        m_sRace = "Planet";
+       m_iStays = -1;
 }
 
 Planet::~Planet(){
@@ -30,7 +31,7 @@ Planet::~Planet(){
 
 //////////////////////////////////////////////////////////////////////////
 //
-unsigned Planet::planetScore(int tick = 0) const
+unsigned Planet::planetScore(int tick) const
 {
        int ticks = m_Score.size();
        if (ticks == 0)
@@ -46,7 +47,7 @@ unsigned Planet::planetScore(int tick = 0) const
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::setPlanetScore(unsigned number, int tick = 0)
+void Planet::setPlanetScore(unsigned number, int tick)
 {
        int earlier = 0;
        int ticks = m_Score.size();
@@ -63,7 +64,7 @@ void Planet::setPlanetScore(unsigned number, int tick = 0)
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::addPlanetScore(unsigned number, int tick = 0)
+void Planet::addPlanetScore(unsigned number, int tick)
 {
        int earlier = 0;
        int ticks = m_Score.size();
@@ -79,7 +80,7 @@ void Planet::addPlanetScore(unsigned number, int tick = 0)
 }
 //////////////////////////////////////////////////////////////////////////
 //
-int Planet::roids(std::string type, int tick = 0) const
+int Planet::roids(std::string type, int tick) const
 {
        // const... I would like [] as for const types: int ticks = m_Roids[type].size();
        
@@ -107,7 +108,7 @@ int Planet::roids(std::string type, int tick = 0) const
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::setRoids(std::string type, int number, int tick = 0)
+void Planet::setRoids(std::string type, int number, int tick)
 {
        int ticks = m_Roids[type].size();
        int roids = 0;
@@ -121,7 +122,7 @@ void Planet::setRoids(std::string type, int number, int tick = 0)
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::takeRoids(std::string type, int number, int tick = 0)
+void Planet::takeRoids(std::string type, int number, int tick)
 {
        int ticks = m_Roids[type].size();
        int roids = 0;
@@ -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(-1000*number, tick);
 }
 
 
@@ -143,6 +147,15 @@ 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();
+
+       m_Report.clear();
+
        for(int tick = 1; skipped < 20; ++tick)
        {
                //See who's in the battle at the current tick
@@ -174,7 +187,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 +199,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 +211,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,75 +223,114 @@ 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)
 {
+       if (planetScore(tick - 1) > 0)
+               setCapping(float(planetScore(tick - 1)) / hostile->score() /  10, tick);
+       else
+               setCapping(0, tick);
+                       
+       //FIXME: remove this line later.. map<string, int> pods;
+
+
+       //FIXME: Need to change this and allow multiple shiptypes with the same initiative. 
+       //FIXME: Looks like I don't have to.. 
        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;
 
+       
+
        for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
        {
                Fleet* hostiletemp = new Fleet(*hostile);
                Planet* friendlytemp = new Planet(*friendly);
                
                string unittype = i->second;
+               int init = i->first;
+
+               int freefriendly = friendly->freeFleet(unittype, 1);
+               int freehostile = hostile->freeFleet(unittype, 1);
+
+               if ( freefriendly <= 0 && freehostile <= 0)
+                       continue;
 
                //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
+       
+               map<string, int> friendlyhits;
+               map<string, int> hostilehits;
 
                if (s_Units[unittype].type() == "EMP")  
                {
-                       hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
-                       friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1));
+                       hostiletemp->takeEMP(unittype, freefriendly, friendlyhits);
+                       friendlytemp->takeEMP(unittype, freehostile, hostilehits);
                }
                else if (s_Units[unittype].type() == "Steal")
                {
-                       hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]);
-                       friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]);
+                       hostiletemp->takeShoot(unittype, freefriendly, stealfriendly[unittype]);
+                       friendlytemp->takeShoot(unittype, freehostile, stealhostile[unittype]);
+                       
+                       friendlyhits = stealfriendly[unittype];
+                       hostilehits = stealhostile[unittype];
+
+                       friendlytemp->calculateLostStealships(unittype, stealfriendly[unittype], 1);
+                       hostiletemp->calculateLostStealships(unittype, stealhostile[unittype], 1);
                }
                else
                {
-                       map<string, int> temp;
-                       hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp);
-                       friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp);
+                       hostiletemp->takeShoot(unittype, freefriendly, friendlyhits);
+                       friendlytemp->takeShoot(unittype, freehostile, hostilehits);
                }
 
+
                if (s_Units[unittype].type() == "Pod")
                {
-                       if (planetScore(tick - 1) > 0)
-                       {
-                               setCapping(float(planetScore(tick - 1)) / hostile->score() /  10, tick);
-                       
-                               cerr << "Capping is: " << capping(tick) << 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(tick) > 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(tick) * roids(roid->first, tick - 1);
-                                               int freepods = hostiletemp->freeFleet(unittype, 1);
+                                       int caproids = int(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 << hostiletemp->freePodGuns(1) << " free pod guns available\n";
+                               
+                                       if (hostiletemp->freePodGuns(1) <= 0)
+                                               break;
+                                       if (hostiletemp->freePodGuns(1) < caproids)
+                                               caproids = hostiletemp->freePodGuns(1);
                
-                                               cerr << caproids << " roids stolen\n";
+                                       cerr << caproids << " roids stolen\n";
 
-                                               takeRoids(roid->first, caproids, tick);
+                                       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);
-                                               
-                                               cerr << caproids << " stolen " << roid->first << " roids\n";
-                                       }
+                                       /*FIXME: REMOVE THIS BLOCK LATER
+                                       //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);
+                                       hostiletemp->usePodGuns(1, caproids);
+                                       cerr << caproids << " stolen " << roid->first << " roids\n";
                                }
                        }
+
+                       //FIXME: This is ugly and I want to change this, but as long as pods shoot last it works.. 
+                       *friendly = *friendlytemp;
+                       delete friendlytemp;
+                       *hostile = *hostiletemp;
+                       delete hostiletemp;
+
+                       return;
                }
+               m_Report[tick][init]["Friendly"][unittype] = friendlyhits;
+               m_Report[tick][init]["Friendly"][unittype]["000"] = freefriendly;
+               m_Report[tick][init]["Hostile"][unittype] = hostilehits;
+               m_Report[tick][init]["Hostile"][unittype]["000"] = freehostile;
 
                //set the the objects so they point at the modified objects
                *friendly = *friendlytemp;
@@ -276,9 +338,13 @@ 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)
+//////////////////////////////////////////////////////////////////////////
+//
+void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick)
 {
        for (vector<Fleet*>::iterator i = friendly.begin(); i != friendly.end(); ++i)
        {
@@ -292,7 +358,7 @@ void Planet::calculateScoreLoss(std::vector<Fleet*> friendly, int tick = 1)
 
 //////////////////////////////////////////////////////////////////////////
 //
-float Planet::capping(int tick = 0)
+float Planet::capping(int tick)
 {
        int ticks = m_Capping.size();
 
@@ -306,7 +372,7 @@ float Planet::capping(int tick = 0)
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::setCapping(float capping, int tick = 0)
+void Planet::setCapping(float capping, int tick)
 {
        int ticks = m_Capping.size();
 
@@ -317,6 +383,15 @@ void Planet::setCapping(float capping, int tick = 0)
 
        if (capping <= 0.15 && capping >= 0)
                m_Capping[tick] = capping;
-       else
+       else if (capping >= 0.15)
                m_Capping[tick] = 0.15;
+       else
+               m_Capping[tick] = 0;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+ReportList Planet::report() const
+{
+       return m_Report;
 }