#include "fleet.h"
#include <iostream>
+#include <cstdlib>
using namespace std;
//Static variables
{
s_Units = units;
- /*
+
for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); i++)
{
<< s_Units[(*i).first].ETA() << "\t"
<< s_Units[(*i).first].type() << endl;
}
- */
+
}
//////////////////////////////////////////////////////////////////////////
//
void Fleet::setFleet(string unittype, int number, int tick = 0)
{
- if (m_Fleet[unittype].size() <= tick)
+ int earlier = 0;
+ int ticks = m_Fleet[unittype].size();
+
+ if (ticks != 0)
+ earlier = m_Fleet[unittype][ticks - 1];
+
+ for (int i = ticks; i <= tick; ++i)
{
- m_Fleet[unittype].push_back(number);
- return;
+ m_Fleet[unittype].push_back(earlier);
}
m_Fleet[unittype][tick] = number;
}
--ticks;
if (ticks < tick)
- m_Fleet[unittype][ticks];
+ return m_Fleet[unittype][ticks];
return m_Fleet[unittype][tick];
}
for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
{
- // FIXTHIS!! m_Fleet[i->first][0] += j->fleet(i->first, tick);
+ m_Fleet[i->first][0] += (*j)->fleet(i->first, tick);
}
}
}
//
void Fleet::distributeLossesGains(std::vector<Fleet*> fleets, int tick = 0)
{
+ for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
+ {
+ string unittype = i->first;
+ if (m_Fleet[unittype].size() < 1)
+ break;
+ if (m_Fleet[unittype][0] == 0)
+ break;
+
+ int totallost = m_Fleet[unittype][1] - m_Fleet[unittype][0];
+
+ for (vector<Fleet*>::iterator j = fleets.begin(); j != fleets.end(); ++j)
+ {
+ int lost = totallost * ( (*j)->fleet(unittype, tick - 1) / m_Fleet[unittype][0] );
+ (*j)->setFleet(unittype, (*j)->fleet(unittype, tick - 1) + lost, tick);
+ }
+ }
}
//////////////////////////////////////////////////////////////////////////
//
int Fleet::freeFleet(std:: string unittype, int tick = 0)
{
- if (m_Fleet[unittype].size() == 0)
- return 0;
- if (m_BlockedFleet[unittype].size() < tick)
- return m_Fleet[unittype][tick];
+ int bticks = m_BlockedFleet[unittype].size();
+
+ --bticks;
- return m_Fleet[unittype][tick] - m_BlockedFleet[unittype][tick];
+ if (bticks < tick)
+ return fleet(unittype, tick);
+
+
+ int free = fleet(unittype,tick) - m_BlockedFleet[unittype][tick];
+ if (free < 0)
+ return 0;
+ return free;
}
//
void Fleet::takeShoot(std::string unittype, int number, std::map<std::string, int>& hitunits)
{
+
+ cerr << unittype << ": " << number << " in number\n";
+ int guns = s_Units[unittype].guns() * number;
+ if (guns == 0)
+ return;
+
+ cerr << unittype << " with " << guns << " guns\n";
+
+ int 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)
+ {
+ 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)
+ break;
+
+ 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];
+ }
+
+ }
+
+ 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)
+ {
+ int maxguns = (*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();
+ float k = maxguns;
+
+ int blaha = 0;
+ cerr << "Targets agility: " << s_Units[j->first].agility() << endl;
+ cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl;
+ while (k > 0)
+ {
+
+ if (*(j->second) <= 0)
+ break;
+ k -= 100/(25+s_Units[unittype].weaponSpeed() - s_Units[j->first].agility());
+ m_Armor[j->first] -= s_Units[unittype].power();
+ if (m_Armor[j->first] <= 0)
+ {
+ 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
+ gunsleft -= maxguns - k;
+ }
+ }
+ }
}
//////////////////////////////////////////////////////////////////////////
//
void Fleet::takeEMP(std::string unittype, int number)
{
+
}
//////////////////////////////////////////////////////////////////////////