]> ruin.nu Git - hbs.git/blobdiff - bs/fleet.cpp
some
[hbs.git] / bs / fleet.cpp
index bab852f0bf704b941a00b464771ac7d4fd37960d..73923b5c22449e43c8702fdf9b376be5a39c201a 100644 (file)
@@ -119,11 +119,11 @@ 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"
-/*                << s_Units[(*i).first].race() <<"\t"
+                  << s_Units[(*i).first].race() <<"\t"
                   << s_Units[(*i).first].unitClass() << "\t"
                   << s_Units[(*i).first].target(0) << "\t"
                   << s_Units[(*i).first].target(1) << "\t"
@@ -137,9 +137,10 @@ void Fleet::setUnits(UnitList& units)
                   << s_Units[(*i).first].EMP() << "\t"
                   << s_Units[(*i).first].totRes() << "\t"
                   << s_Units[(*i).first].fuel() << "\t"
-                  << s_Units[(*i).first].ETA() << "\t"*/
+                  << s_Units[(*i).first].ETA() << "\t"
                   << s_Units[(*i).first].type() << endl;
           }
+          */
 }
 
 //////////////////////////////////////////////////////////////////////////
@@ -315,12 +316,12 @@ void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick)
                {
                        int fl1 = (*j)->fleet(unittype, tick - 1);
                        float part = float(fl1) / fleet(unittype, 0) ;
-                       int lost =  totallost * part;
+                       int lost =  int(totallost * part);
                        (*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);
+                       lost = int(part * blockedFleet(unittype, 0));
 
                        cerr << (*j)->name() << " got " << lost << " blocked " << unittype  << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; 
 
@@ -331,7 +332,7 @@ void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick)
 
 //////////////////////////////////////////////////////////////////////////
 //
-std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int stays, int tick)
+std::vector<Fleet*> Fleet::calculateSide(std::vector<Fleet*> fleets, int tick)
 {
        vector<Fleet*> fl;
        for (vector<Fleet*>::iterator i = fleets.begin(); i != fleets.end(); ++i)
@@ -379,55 +380,27 @@ void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, in
        if (guns == 0)
                return;
 
-       cerr << number << " " << unittype << ": with " << guns << " guns\n";
-
        float 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)
+       for (int count = 0; count < 3; ++count)
        {
                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)
-                                       continue;
-
-
-                               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];
-                               }
-                       }
+                       int total = 0;
+                       map<string, int*> targets = findTargets(ta,total, 0);
 
                        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)
                        {
                                float maxguns = float((*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();
                                double k = maxguns;
 
-                               //cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
-                               //cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
                                while (k > 0)   
                                {
 
@@ -438,8 +411,6 @@ void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, in
                                        int agil = s_Units[j->first].agility();
 
                                        k -= float(100)/(25 + wpsp - agil);
-                                       //cout << "Used " << blaha << " guns to hit with one shot.\n";
-                                       //cout << "WPSP: " << wpsp << "\nAgil: " << agil << endl;
 
                                        m_Armor[j->first] -= s_Units[unittype].power();
                                        if (m_Armor[j->first] <= 0)
@@ -447,25 +418,9 @@ void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, in
                                                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
@@ -477,63 +432,29 @@ void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, in
 
 //////////////////////////////////////////////////////////////////////////
 //
-void Fleet::takeEMP(std::string unittype, int number)
+void Fleet::takeEMP(std::string unittype, int number, std::map<std::string, int>& hitunits)
 {
        int guns = s_Units[unittype].guns() * number;
        if (guns == 0)
                return;
 
-       cerr << unittype << ": with " << guns << " guns\n";
-
        float 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)
+       for (int count = 0; count < 3; ++count)
        {
                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 (j->second.type() == "PDS")
-                                       continue;
-
-                               if (freeFleet(j->first, 1) <= 0)
-                                       continue;
-
-                               if (m_Fleet[j->first].size() == 0)
-                                       continue;
-
-                               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];
-                               }
-
-                       }
+                       int total = 0;
+                       map<string, int*> targets = findTargets(ta, total, 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)
                        {
-                               float maxguns = float((*j->second))/total * guns;
-                               cerr << "Now shooting at target: " << j->first << endl;
-
+                               float maxguns = float((*j->second))/total * gunsleft;
                                double k = maxguns;
-
                                int hits = 0;
 
                                while (k > 0)   
@@ -546,10 +467,9 @@ void Fleet::takeEMP(std::string unittype, int number)
 
                                        k -= float(100)/(100-eres);
                                        hits++;
+                                       hitunits[j->first]++;
                                        blockFleet(j->first, 1);
                                }
-
-                               cerr << hits << " units of type: " << j->first << " blocked\n";
                                if (k <= 0)
                                        gunsleft -= maxguns;
                                else
@@ -704,7 +624,7 @@ void Fleet::distributeCappedRoids(std::vector<Fleet*> fleets, int tick)
                {
                        unsigned fl1 = (*j)->score(tick - 1);
                        float part = float(fl1) / score(0) ;
-                       int lost =  totcapped * part;
+                       int lost =  int(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;
 
@@ -743,10 +663,10 @@ void Fleet::calculateLostStealships(string unittype, std::map<std::string, int>
        int stealscore = 0;
        for (map<string, int>::iterator i = stolen.begin(); i != stolen.end(); ++i)
        {
-               stealscore += stolen[i->first] * (s_Units[i->first].totRes() / 10.0);   
+               stealscore += int(stolen[i->first] * (s_Units[i->first].totRes() / 10.0));      
        }
 
-       int lost = stealscore / (s_Units[unittype].totRes() / 10.0);
+       int lost = int(stealscore / (s_Units[unittype].totRes() / 10.0));
 
        cerr << "Lost " << lost << " " << unittype << " due to stealing ships worth: " << stealscore << endl; 
        killFleet(unittype, lost, tick);
@@ -766,7 +686,7 @@ void Fleet::distributeStolenShips(std::map<std::string, std::map<std::string, in
                {
                        for (vector<Fleet*>::iterator k = fleets.begin(); k != fleets.end(); ++k)
                        {
-                               int stolen = float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second;
+                               int stolen = int(float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second);
                                (*k)->addFleet(j->first, stolen, tick);
                        }
                }
@@ -794,7 +714,7 @@ void Fleet::calculateSalvage()
                                continue;
                        cerr << "(" << tick << ":" << fleet(i->first, tick) << ") ";
                        for (map<string, int>::iterator k = res.begin(); k != res.end(); ++k)
-                               addResource(k->first, lostunits * k->second * 0.25, tick);
+                               addResource(k->first, int(lostunits * k->second * 0.25), tick);
                }
        }
 }
@@ -866,3 +786,28 @@ void Fleet::usePodGuns(int tick, int guns)
        }
        m_UsedPodGuns[tick] += guns;
 }
+
+//////////////////////////////////////////////////////////////////////////
+//
+std::map<std::string, int*> Fleet::findTargets(std::string targetClass,int& total, int attackerType)
+{
+       map<string, int*> targets;
+
+       for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
+       {
+               if (m_Fleet[i->first].size() == 0)
+                       continue;
+
+               if (m_Fleet[i->first].size() == 1 )
+                       m_Fleet[i->first].push_back(m_Fleet[i->first][0]);
+
+               if (m_Fleet[i->first][1] > 0  && ( targetClass == i->second.unitClass() || targetClass == "All"))
+               {
+                       if (i->second.unitClass() == "PDS" && attackerType == 1)
+                               continue;
+                       targets[i->first] = &m_Fleet[i->first][1];
+                       total += m_Fleet[i->first][1];
+               }
+       }
+       return targets;
+}