X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=bs%2Ffleet.cpp;h=1cdc04fb9d1a3e49e022d2a8ca4fc68175f81090;hb=1c8c7800411f2491d9be16c0f1515a91525f2e56;hp=f0fb8e1dee9d07d45c42c8d0784d65d6f8b5c907;hpb=2fd4fff0f5eef1d64527b202924f16f2242691dc;p=hbs.git diff --git a/bs/fleet.cpp b/bs/fleet.cpp index f0fb8e1..1cdc04f 100644 --- a/bs/fleet.cpp +++ b/bs/fleet.cpp @@ -118,7 +118,7 @@ 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" @@ -139,7 +139,7 @@ void Fleet::setUnits(UnitList& units) << s_Units[(*i).first].ETA() << "\t" << s_Units[(*i).first].type() << endl; } - +*/ } ////////////////////////////////////////////////////////////////////////// @@ -171,8 +171,13 @@ unsigned Fleet::score(int tick = 0) const for (FleetList::const_iterator i = m_Fleet.begin(); i != m_Fleet.end(); ++i) { - if (i->second.size() >= tick) - break; + 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; } @@ -212,6 +217,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) @@ -223,7 +260,10 @@ void Fleet::addToThis(std::vector fleets, int tick = 0) for (vector::iterator j = fleets.begin(); j != fleets.end(); ++j) { - m_Fleet[i->first][0] += (*j)->fleet(i->first, tick); + 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; } } } @@ -235,17 +275,35 @@ 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) - break; - if (m_Fleet[unittype][0] == 0) - break; + continue; + if (m_Fleet[unittype][0] == 0) + continue; + + + int totallost = fleet(unittype,1) - fleet(unittype, 0); - int totallost = m_Fleet[unittype][1] - m_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 lost = totallost * ( (*j)->fleet(unittype, tick - 1) / m_Fleet[unittype][0] ); + 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); } } } @@ -258,7 +316,10 @@ std::vector Fleet::calculateSide(std::vector fleets, int stays = for (vector::iterator i = fleets.begin(); i != fleets.end(); ++i) { if (( tick - (*i)->ETA()) >= 0 && (tick - (*i)->ETA()) < stays) + { fl.push_back((*i)); + cerr << "Using fleet " << (*i)->name() << " for tick " << tick << endl; + } else if ((*i)->name() == "Home Planet") fl.push_back((*i)); } @@ -289,18 +350,19 @@ int Fleet::freeFleet(std:: string unittype, int tick = 0) void Fleet::takeShoot(std::string unittype, int number, std::map& hitunits) { - cerr << unittype << ": " << number << " in number\n"; - int guns = s_Units[unittype].guns() * number; + float guns = s_Units[unittype].guns() * number; + + if (guns == 0) return; - cerr << unittype << " with " << guns << " guns\n"; + cerr << number << " " << unittype << ": with " << guns << " guns\n"; - int gunsleft = guns; + 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; + cerr << "Shooting at target class: " << ta << endl; while (gunsleft > 0) { @@ -310,7 +372,7 @@ void Fleet::takeShoot(std::string unittype, int number, std::mapfirst].size() == 0) - break; + continue; if (m_Fleet[j->first].size() == 1) m_Fleet[j->first].push_back(m_Fleet[j->first][0]); @@ -320,7 +382,7 @@ void Fleet::takeShoot(std::string unittype, int number, std::mapfirst][1] > 0 && ( ta == j->second.unitClass() || ta == "All")) { - cerr << "Looking at target: " << j->first << endl; + // cerr << "Looking at target: " << j->first << endl; targets[j->first] = &m_Fleet[j->first][1]; } @@ -335,22 +397,28 @@ void Fleet::takeShoot(std::string unittype, int number, std::map::iterator j = targets.begin(); j != targets.end(); ++j) { - int maxguns = (*j->second)/total * guns; - cerr << "Now shooting at target: " << j->first << endl; + 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(); - float k = maxguns; + double k = maxguns; - int blaha = 0; - cerr << "Targets agility: " << s_Units[j->first].agility() << endl; - cerr << "Weaponspeed: " << s_Units[unittype].weaponSpeed() << endl; + //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()); + + 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) { @@ -389,17 +457,96 @@ void Fleet::takeShoot(std::string unittype, int number, std::map::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 (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) <= 0) + 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) { - 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(m_Fleet[unittype][m_Fleet[unittype].size()] - number); - return; + m_Fleet[unittype].push_back(earlier); } m_Fleet[unittype][tick] -= number; } @@ -409,11 +556,28 @@ void Fleet::killFleet(std::string unittype, int number, int tick = 0) void Fleet::setResource(std::string type, int number, int tick = 0) { - if (m_Resources[type].size() <= tick) + 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 = 0; + + if (ticks > 0) + latest = m_Resources[type][ticks - 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 @@ -442,3 +606,77 @@ int Fleet::resource(std::string type, int tick = 0) const 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; + } + } +} + +////////////////////////////////////////////////////////////////////////// +// +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); + } + } +} + +