+ 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)
+{
+
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::killFleet(std::string unittype, int number, int tick = 0)
+{
+ if (m_Fleet[unittype].size() <= tick)
+ {
+ m_Fleet[unittype].push_back(m_Fleet[unittype][m_Fleet[unittype].size()] - number);
+ return;
+ }
+ m_Fleet[unittype][tick] -= number;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::setResource(std::string type, int number, int tick = 0)
+{
+
+ if (m_Resources[type].size() <= tick)
+ m_Resources[type].push_back(number);
+ m_Resources[type][tick] = number;
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+int Fleet::resource(std::string type, int tick = 0) const
+{
+ vector<int>const* resource = 0;
+ for (ResourceList::const_iterator i = m_Resources.begin(); i != m_Resources.end(); ++i)
+ {
+ if (i->first == type)
+ {
+ resource = &i->second;
+ break;
+ }
+ }
+ if (resource == 0)
+ return 0;
+
+ int ticks = resource->size();
+
+ if( ticks == 0)
+ return 0;
+
+ --ticks;
+
+ if (ticks < tick)
+ return resource->at(ticks);
+ return resource->at(tick);
+}
+
+//////////////////////////////////////////////////////////////////////////
+//
+void Fleet::printFleet()
+{
+ for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i)
+ {
+ for (int tick = 0; tick < 5 ;++tick)
+ {
+ int num = fleet(i->first, tick);
+
+ if (num <= 0)
+ break;
+ cerr << num << " " << i->first << " during tick: " << tick << endl;
+ }
+ }
+}