X-Git-Url: https://ruin.nu/git/?p=hbs.git;a=blobdiff_plain;f=bs%2Ffleet.cpp;h=85304bb2bfcb6e642826764749ca1bad9a2ea4be;hp=a7ebe5cf0f7b610550b6c759ab1a0ea66aabe350;hb=b308a9445cfe1883ea8068d3f4d1a04bfcff9aa8;hpb=dc90594097a886eee49e9bfe45da6d4f9765179a diff --git a/bs/fleet.cpp b/bs/fleet.cpp index a7ebe5c..85304bb 100644 --- a/bs/fleet.cpp +++ b/bs/fleet.cpp @@ -18,17 +18,20 @@ #include "fleet.h" #include +#include using namespace std; //Static variables map > Fleet::s_Races; -map Fleet::s_Units; +UnitList Fleet::s_Units; Fleet::Fleet() { m_iETA = 0; - m_sRace = "Generic"; + m_iStays = 3; + m_sRace = "Cathaar"; } + Fleet::~Fleet(){ } @@ -41,7 +44,7 @@ void Fleet::setName(string sName) ////////////////////////////////////////////////////////////////////////// // -string Fleet::Name() +string Fleet::name() const { return m_sName; } @@ -52,7 +55,7 @@ string Fleet::Name() * s_Races and checks if it finds the race it returns true, if it reaches * the end without finding it it returns false. */ -bool Fleet::setRace(std::string sRace) +bool Fleet::setRace(string sRace) { m_sRace = sRace; for (map >::iterator i = s_Races.begin(); i != s_Races.end(); i++) @@ -65,7 +68,7 @@ bool Fleet::setRace(std::string sRace) ////////////////////////////////////////////////////////////////////////// // -string Fleet::Race() +string Fleet::race() const { return m_sRace; } @@ -75,13 +78,14 @@ string Fleet::Race() /** This function iterates through m_Fleet and adds all numbers together to * produce a total. */ -int Fleet::NumberOfShips() +int Fleet::numberOfShips() const { int total = 0; - for (map >::iterator i = m_Fleet.begin(); i != m_Fleet.end(); i++) + for (map >::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i) { - total += m_Fleet[(*i).first][0]; + if (i->second.size() != 0) + total += i->second[0]; } return total; @@ -96,43 +100,722 @@ void Fleet::setETA(int eta) ////////////////////////////////////////////////////////////////////////// // -int Fleet::ETA() +int Fleet::ETA() const { return m_iETA; } ////////////////////////////////////////////////////////////////////////// // -void Fleet::setRaces(std::map >& races) +void Fleet::setRaces(map >& races) { s_Races = races; } ////////////////////////////////////////////////////////////////////////// // -void Fleet::setUnits(map& units) +void Fleet::setUnits(UnitList& units) { s_Units = units; - for (map::iterator i = s_Units.begin(); i != s_Units.end(); i++) - { - cerr << s_Units[(*i).first].Name() << "\t" - << s_Units[(*i).first].Race() <<"\t" - << s_Units[(*i).first].Class() << "\t" - << s_Units[(*i).first].Target(0) << "\t" - << s_Units[(*i).first].Target(1) << "\t" - << s_Units[(*i).first].Target(2) << "\t" - << s_Units[(*i).first].Initiative() << "\t" - << s_Units[(*i).first].Agility() << "\t" - << s_Units[(*i).first].WeaponSpeed() << "\t" - << s_Units[(*i).first].Guns() << "\t" - << s_Units[(*i).first].Power() << "\t" - << s_Units[(*i).first].Armor() << "\t" + +/* + 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].unitClass() << "\t" + << s_Units[(*i).first].target(0) << "\t" + << s_Units[(*i).first].target(1) << "\t" + << s_Units[(*i).first].target(2) << "\t" + << s_Units[(*i).first].initiative() << "\t" + << s_Units[(*i).first].agility() << "\t" + << s_Units[(*i).first].weaponSpeed() << "\t" + << s_Units[(*i).first].guns() << "\t" + << s_Units[(*i).first].power() << "\t" + << s_Units[(*i).first].armor() << "\t" << s_Units[(*i).first].EMP() << "\t" - << s_Units[(*i).first].TotRes() << "\t" - << s_Units[(*i).first].Fuel() << "\t" + << s_Units[(*i).first].totRes() << "\t" + << s_Units[(*i).first].fuel() << "\t" << s_Units[(*i).first].ETA() << "\t" - << s_Units[(*i).first].Type() << endl; + << s_Units[(*i).first].type() << endl; + } +*/ +} + +////////////////////////////////////////////////////////////////////////// +// +const map >& Fleet::Races() +{ + return s_Races; +} + +////////////////////////////////////////////////////////////////////////// +// +const UnitList& Fleet::Units() +{ + return s_Units; +} + +////////////////////////////////////////////////////////////////////////// +// +vector Fleet::RacesAllowed() const +{ + return s_Races[m_sRace]; +} + +////////////////////////////////////////////////////////////////////////// +// +unsigned Fleet::score(int tick = 0) const +{ + unsigned tot_score = 0; + + for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i) + { + int ticks = i->second.size(); + if (ticks == 0) + continue; + --ticks; + if ( ticks < tick) + tot_score += i->second[ticks] * s_Units[i->first].totRes() / 10; + else + tot_score += i->second[tick] * s_Units[i->first].totRes() / 10; + } + + return tot_score; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::setFleet(string unittype, int number, int tick = 0) +{ + 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(earlier); + } + m_Fleet[unittype][tick] = number; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::addFleet(std::string unittype, int number, int tick = 0) +{ + 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(earlier); + } + m_Fleet[unittype][tick] += number; +} + +////////////////////////////////////////////////////////////////////////// +// +int Fleet::fleet(string unittype, int tick = 0) +{ + + int ticks = m_Fleet[unittype].size(); + if (ticks == 0) + return 0; + + if (tick < 0) + return m_Fleet[unittype][0]; + + --ticks; + + if (ticks < tick) + return m_Fleet[unittype][ticks]; + + return m_Fleet[unittype][tick]; +} + +////////////////////////////////////////////////////////////////////////// +// +int Fleet::blockedFleet(std::string unittype, int tick = 0) +{ + int ticks = m_BlockedFleet[unittype].size(); + if (ticks == 0) + return 0; + + --ticks; + + if (ticks < tick) + return 0; + + return m_BlockedFleet[unittype][tick]; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::setBlockedFleet(std::string unittype, int number, int tick = 0) +{ + int ticks = m_BlockedFleet[unittype].size(); + + for (int i = ticks; i <= tick; ++i) + { + m_BlockedFleet[unittype].push_back(0); + } + m_BlockedFleet[unittype][tick] = number; + + cerr << "This fleet got " << m_BlockedFleet[unittype][tick] << " blocked units tick: " << tick << endl; + +} + +////////////////////////////////////////////////////////////////////////// +//FIXME +void Fleet::addToThis(std::vector fleets, int tick = 0) +{ + for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i) + { + if (m_Fleet[i->first].size() == 0) + m_Fleet[i->first].push_back(0); + + for (vector::iterator j = fleets.begin(); j != fleets.end(); ++j) + { + int num = (*j)->fleet(i->first, tick); + m_Fleet[i->first][0] += num; + if (num > 0) + cerr << (*j)->name() << " adding " << num << " units of type " << i->first << endl; + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::distributeLossesGains(std::vector 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) + continue; + if (m_Fleet[unittype][0] == 0) + continue; + + + int totallost = fleet(unittype,1) - fleet(unittype, 0); + + + cerr << "Distributing type: " << unittype << " with a total loss of " << totallost << " units" << endl; + + cerr << "Total number of units before: " << fleet(unittype, 0) << " and after : " << fleet(unittype, 1) << endl; + + for (vector::iterator j = fleets.begin(); j != fleets.end(); ++j) + { + int fl1 = (*j)->fleet(unittype, tick - 1); + float part = float(fl1) / fleet(unittype, 0) ; + int lost = 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); + + cerr << (*j)->name() << " got " << lost << " blocked " << unittype << ", the total number of blocked ships was: " << blockedFleet(unittype, 0) << endl; + + (*j)->setBlockedFleet(unittype, lost, tick); + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +std::vector Fleet::calculateSide(std::vector fleets, int stays = 0, int tick = 0) +{ + vector fl; + for (vector::iterator i = fleets.begin(); i != fleets.end(); ++i) + { + if ((*i)->stays() == 0) + continue; + else if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < (*i)->stays()) + { + fl.push_back((*i)); + cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl; + } + else if ((*i)->stays() < 0) + fl.push_back((*i)); + } + return fl; +} + +////////////////////////////////////////////////////////////////////////// +// +int Fleet::freeFleet(std:: string unittype, int tick = 0) +{ + int bticks = m_BlockedFleet[unittype].size(); + + --bticks; + + 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& hitunits) +{ + + float guns = s_Units[unittype].guns() * number; + + + if (guns == 0) + return; + + cerr << number << " " << unittype << ": with " << guns << " guns\n"; + + float gunsleft = guns; + for (int count = 0; count < 3; ++count)//vector::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 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]; + } + + } + + if (targets.size() == 0) + break; + + int total = 0; + for (map::iterator j = targets.begin(); j != targets.end(); ++j) + total += (*j->second); + + for (map::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) + { + + if (*(j->second) <= 0) + break; + + int wpsp = s_Units[unittype].weaponSpeed(); + 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) + { + 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) +{ + 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::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 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]; + } + + } + + if (targets.size() == 0) + break; + + int total = 0; + for (map::iterator j = targets.begin(); j != targets.end(); ++j) + total += (*j->second); + + for (map::iterator j = targets.begin(); j != targets.end(); ++j) + { + float maxguns = float((*j->second))/total * guns; + cerr << "Now shooting at target: " << j->first << endl; + + double k = maxguns; + + int hits = 0; + + while (k > 0) + { + + if (*(j->second) <= blockedFleet(j->first, 1)) + break; + + int eres = s_Units[j->first].EMP(); + + k -= float(100)/(100-eres); + hits++; + blockFleet(j->first, 1); + } + + cerr << hits << " units of type: " << j->first << " blocked\n"; + if (k <= 0) + gunsleft -= maxguns; + else + gunsleft -= maxguns - k; + } + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::killFleet(std::string unittype, int number, int tick = 0) +{ + 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(earlier); + } + m_Fleet[unittype][tick] -= number; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::setResource(std::string type, int number, int tick = 0) +{ + + int ticks = m_Resources[type].size(); + for (int i = ticks; i <= tick; ++i) + m_Resources[type].push_back(number); + m_Resources[type][tick] = number; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::addResource(std::string type, int number, int tick = 0) +{ + + int ticks = m_Resources[type].size(); + int latest = resource(type, tick - 1); + + for (int i = ticks; i <= tick; ++i) + m_Resources[type].push_back(latest); + m_Resources[type][tick] += number; +} + +////////////////////////////////////////////////////////////////////////// +// +int Fleet::resource(std::string type, int tick = 0) const +{ + if (tick < 0) + return 0; + + vectorconst* 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::resetResources() +{ + m_Resources.clear() ; +} + +////////////////////////////////////////////////////////////////////////// +// +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; + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::blockFleet(std::string unittype, int number, int tick = 0) +{ + if (m_BlockedFleet[unittype].size() >= 1) + { + m_BlockedFleet[unittype][0] += number; + if (m_BlockedFleet[unittype].size() > 1) + m_BlockedFleet[unittype][1] += number; + else + m_BlockedFleet[unittype].push_back(m_BlockedFleet[unittype][0]); + } + else + { + m_BlockedFleet[unittype].push_back(number); + m_BlockedFleet[unittype].push_back(number); } } +////////////////////////////////////////////////////////////////////////// +// +void Fleet::distributeCappedRoids(std::vector fleets, int tick = 0) +{ + for (ResourceList::iterator i = m_Resources.begin(); i != m_Resources.end(); ++i) + { + string res = i->first; + + + cerr << "Distributing type: " << res << endl; + for (vector::iterator j = i->second.begin(); j != i->second.end(); ++j) + cout << (*j) << endl; + + if (m_Resources[res].size() < 2) + continue; + if (m_Resources[res][1] == 0) + continue; + + + int totcapped = resource(res,1) - resource(res, 0); + + + cerr << "Distributing type: " << res << " with a total gain of " << totcapped << " roids" << endl; + + cerr << "Total number of roids before: " << resource(res, 0) << " and after : " << resource(res, 1) << endl; + + for (vector::iterator j = fleets.begin(); j != fleets.end(); ++j) + { + unsigned fl1 = (*j)->score(tick - 1); + float part = float(fl1) / score(0) ; + int lost = 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; + + //(*j)->setResource(res, (*j)->resource(res,tick-1) + lost, tick); + (*j)->addResource(res,lost, tick); + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::addFleet(std::map units, int tick = 0) +{ + for (map::iterator i = units.begin(); i != units.end(); ++i) + addFleet(i->first, i->second, tick); +} + +////////////////////////////////////////////////////////////////////////// +// +int Fleet::stays() const +{ + return m_iStays; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::setStays(int ticks) +{ + m_iStays = ticks; +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::calculateLostStealships(string unittype, std::map stolen, int tick = 1) +{ + int stealscore = 0; + for (map::iterator i = stolen.begin(); i != stolen.end(); ++i) + { + stealscore += stolen[i->first] * (s_Units[i->first].totRes() / 10.0); + } + + int lost = stealscore / (s_Units[unittype].totRes() / 10.0); + + cerr << "Lost " << lost << " " << unittype << " due to stealing ships worth: " << stealscore << endl; + killFleet(unittype, lost, tick); +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::distributeStolenShips(std::map > stolen, std::vector fleets, int tick = 0) +{ + for(map >::iterator i = stolen.begin(); i != stolen.end(); ++i) + { + int totalstealers = 0; + for (vector::iterator j = fleets.begin(); j != fleets.end(); ++j) + totalstealers += (*j)->fleet(i->first, tick - 1); + + for (map::iterator j = i->second.begin(); j != i->second.end(); ++j) + { + for (vector::iterator k = fleets.begin(); k != fleets.end(); ++k) + { + int stolen = float ((*k)->fleet(i->first, tick - 1)) / totalstealers * j->second; + (*k)->addFleet(j->first, stolen, tick); + } + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::calculateSalvage() +{ + for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i) + { + + map res = s_Units[i->first].resources(); + + if (i->second.size() > 0) + cerr << endl << i->first << ": "; + + int tick = 0; + for (vector::iterator j = i->second.begin(); j != i->second.end(); ++j, ++tick) + { + int lostunits = fleet(i->first, tick - 1) - fleet(i->first, tick); + + if (lostunits <= 0) + continue; + cerr << "(" << tick << ":" << fleet(i->first, tick) << ") "; + for (map::iterator k = res.begin(); k != res.end(); ++k) + addResource(k->first, lostunits * k->second * 0.25, tick); + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Fleet::resetTicks() +{ + for (FleetList::iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i) + { + if ( i->second.size() < 2) + continue; + + int temp = i->second[0]; + i->second.clear(); + + if (temp > 0) + i->second.push_back(temp); + } + resetResources(); +}