X-Git-Url: https://ruin.nu/git/?p=hbs.git;a=blobdiff_plain;f=bs%2Ffleet.cpp;h=85304bb2bfcb6e642826764749ca1bad9a2ea4be;hp=545ffa2e43d094fe09a3490d85d13dacebc29e49;hb=b308a9445cfe1883ea8068d3f4d1a04bfcff9aa8;hpb=0ffaa1b7fc0af8df728154f0b6e8bf1b77555514 diff --git a/bs/fleet.cpp b/bs/fleet.cpp index 545ffa2..85304bb 100644 --- a/bs/fleet.cpp +++ b/bs/fleet.cpp @@ -28,6 +28,7 @@ UnitList Fleet::s_Units; Fleet::Fleet() { m_iETA = 0; + m_iStays = 3; m_sRace = "Cathaar"; } @@ -201,14 +202,35 @@ void Fleet::setFleet(string unittype, int number, int tick = 0) 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) @@ -217,6 +239,38 @@ int Fleet::fleet(string unittype, int tick = 0) 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) @@ -263,8 +317,15 @@ void Fleet::distributeLossesGains(std::vector fleets, int tick = 0) int fl1 = (*j)->fleet(unittype, tick - 1); float part = float(fl1) / fleet(unittype, 0) ; int lost = totallost * part; - cerr << (*j)->name() << " gaining " << lost << " " << unittype << " since it's " << part * 100 << "% of the whole fleet, and it had : " << fl1 << " units last tick.." << endl; (*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); } } } @@ -276,12 +337,14 @@ std::vector Fleet::calculateSide(std::vector fleets, int stays = vector fl; for (vector::iterator i = fleets.begin(); i != fleets.end(); ++i) { - if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays) + 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)->name() == "Home Planet") + else if ((*i)->stays() < 0) fl.push_back((*i)); } return fl; @@ -335,7 +398,8 @@ void Fleet::takeShoot(std::string unittype, int number, std::mapfirst].size() == 0) continue; - if (m_Fleet[j->first].size() == 1) + + 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; @@ -439,6 +503,9 @@ void Fleet::takeEMP(std::string unittype, int number) if (j->second.type() == "PDS") continue; + if (freeFleet(j->first, 1) <= 0) + continue; + if (m_Fleet[j->first].size() == 0) continue; @@ -465,7 +532,7 @@ void Fleet::takeEMP(std::string unittype, int number) for (map::iterator j = targets.begin(); j != targets.end(); ++j) { - int maxguns = (*j->second)/total * guns; + float maxguns = float((*j->second))/total * guns; cerr << "Now shooting at target: " << j->first << endl; double k = maxguns; @@ -475,7 +542,7 @@ void Fleet::takeEMP(std::string unittype, int number) while (k > 0) { - if (*(j->second) <= 0) + if (*(j->second) <= blockedFleet(j->first, 1)) break; int eres = s_Units[j->first].EMP(); @@ -529,10 +596,7 @@ void Fleet::addResource(std::string type, int number, int tick = 0) { int ticks = m_Resources[type].size(); - int latest = 0; - - if (ticks > 0) - latest = m_Resources[type][ticks - 1]; + int latest = resource(type, tick - 1); for (int i = ticks; i <= tick; ++i) m_Resources[type].push_back(latest); @@ -543,6 +607,9 @@ void Fleet::addResource(std::string type, int number, int tick = 0) // 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) { @@ -567,6 +634,13 @@ int Fleet::resource(std::string type, int tick = 0) const return resource->at(tick); } +////////////////////////////////////////////////////////////////////////// +// +void Fleet::resetResources() +{ + m_Resources.clear() ; +} + ////////////////////////////////////////////////////////////////////////// // void Fleet::printFleet() @@ -611,6 +685,7 @@ void Fleet::distributeCappedRoids(std::vector fleets, int tick = 0) { string res = i->first; + cerr << "Distributing type: " << res << endl; for (vector::iterator j = i->second.begin(); j != i->second.end(); ++j) cout << (*j) << endl; @@ -635,9 +710,112 @@ void Fleet::distributeCappedRoids(std::vector fleets, int tick = 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)->addResource(res, lost, tick); + + //(*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(); +}