]> ruin.nu Git - hbs.git/blobdiff - bs/planet.cpp
last minute bug.. ;P
[hbs.git] / bs / planet.cpp
index 4ae9c59e4d517e87b9047c7eb9b00dee837e3dad..c374a99ff9cc7f726fd18392cad21aeb0890a5b2 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "planet.h"
 
+#include <iostream>
 using namespace std;
 
 Planet::Planet()
@@ -64,6 +65,7 @@ int Planet::roids(std::string type, int tick = 0) const
 
        if( ticks == 0)
                return 0;
+       --ticks;
        if (ticks < tick)
                return roids->at(ticks);
        return roids->at(tick);
@@ -71,18 +73,43 @@ int Planet::roids(std::string type, int tick = 0) const
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Planet::setRoids(std::string type, int number)
+void Planet::setRoids(std::string type, int number, int tick = 0)
 {
-       if (m_Roids[type].size() == 0)
-               m_Roids[type].push_back(number);
-       m_Roids[type][0] = number;
+       int ticks = m_Roids[type].size();
+       int roids = 0;
+       if (m_Roids[type].size() > 0)
+               roids = m_Roids[type][m_Roids[type].size() - 1];
+
+       for (int i = ticks; i <= tick; ++i )
+               m_Roids[type].push_back(roids);
+       m_Roids[type][tick] = number;
 }
 
+//////////////////////////////////////////////////////////////////////////
+//
+void Planet::takeRoids(std::string type, int number, int tick = 0)
+{
+       int ticks = m_Roids[type].size();
+       int roids = 0;
+       if (m_Roids[type].size() > 0)
+               roids = m_Roids[type][m_Roids[type].size() - 1];
+
+       for (int i = ticks; i <= tick; ++i )
+               m_Roids[type].push_back(roids);
+       m_Roids[type][tick] -= number;
+}
+
+
+
 //////////////////////////////////////////////////////////////////////////
 //
 void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile)
 {
-       for(int tick = 1; ; ++tick)
+       if (hostile.size() == 0)
+                       return;
+
+       int skipped = 0;
+       for(int tick = 1; skipped < 20; ++tick)
        {
                //See who's in the battle at the current tick
                vector<Fleet*> friends = calculateSide(friendly, 6, tick);
@@ -90,31 +117,53 @@ void Planet::runBattle(std::vector<Fleet*> friendly, std::vector<Fleet*> hostile
 
                // No idea to calculate anything if noone is there.. ;)
                if (hostiles.size() == 0)
-                       break;
+               {
+                       skipped++;
+                       continue;
+               }
+               else
+                       skipped = 0;
 
                Planet allFriends;
-               allFriends.addToThis(friends);
+               allFriends.addToThis(friends, tick - 1);
+
+               /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
+                       allFriends.setRoids(i->first, roids(i->first, tick));
+
+               allFriends.setPlanetScore(m_iScore);*/
                
                Fleet allHostiles;
-               allHostiles.addToThis(hostiles);
+               allHostiles.addToThis(hostiles, tick - 1);
 
                map<string, map<string, int> > stealfriendly;
                map<string, map<string, int> > stealhostile;
                
-               calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile );
+               calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick );
+
+               //allFriends.printFleet();
 
                allFriends.distributeLossesGains(friends, tick);
-               allHostiles.distributeLossesGains(friends, tick);
+               allHostiles.distributeLossesGains(hostiles, tick);
+               allHostiles.distributeCappedRoids(hostiles, tick);
+
+       /*      for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i)
+                       setRoids(i->first, roids(i->first, 1), tick);*/
+       }
+
+       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)
+                       cout << i->first << " roids : " << (*j) << endl;
        }
 }
 
 //////////////////////////////////////////////////////////////////////////
 //
-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 )
+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)
 {
        map<int, string> unitsinit; // order units after their ininitiative
        for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
-               unitsinit[i->second.ETA()] = i->first;
+               unitsinit[i->second.initiative()] = i->first;
 
        for (map<int, string>::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i)
        {
@@ -123,6 +172,8 @@ void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string,
                
                string unittype = i->second;
 
+               //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl;
+
                if (s_Units[unittype].type() == "EMP")  
                {
                        hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1));
@@ -142,27 +193,49 @@ void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map<std::string,
 
                if (s_Units[unittype].type() == "Pod")
                {
-                       float capping = friendly->m_iScore / hostile->score() /  10;
-      for (RoidList::iterator roids = m_Roids.begin(); roids != m_Roids.end(); ++roids)
+                       if (m_iScore > 0)
                        {
-                               int caproids = capping * roids->second[0];
-                               int freepods = hostiletemp->freeFleet(unittype, 1);
-                               
-                               if (freepods == 0)
-                                       break;
-                               if (freepods < caproids)
-                                       caproids = caproids - freepods;
-                                       
-                               roids->second.push_back(roids->second[0] - caproids);
-                               hostiletemp->killFleet(unittype, caproids, 1);
+                               float capping = float(m_iScore) / hostile->score() /  10;
+                       
+                               cerr << "Capping is: " << capping << endl;
+       
+                               if (capping > 0.15)
+                                       capping = 0.15;
+                               if (capping > 0)
+                               {
+                                       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);
+               
+                                               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);
+               
+                                               //FIXME: Going to move this to the distribute roids section instead..
+                                               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";
+                                       }
+                               }
                        }
                }
 
                //set the the objects so they point at the modified objects
-               delete friendly;
-               friendly = friendlytemp;
-               delete hostile;
-               hostile = hostiletemp;
+               *friendly = *friendlytemp;
+               delete friendlytemp;
+               *hostile = *hostiletemp;
+               delete hostiletemp;
        }
 }