X-Git-Url: https://ruin.nu/git/?p=hbs.git;a=blobdiff_plain;f=bs%2Fplanet.cpp;h=c374a99ff9cc7f726fd18392cad21aeb0890a5b2;hp=03c45370700acb53018ce2c342cebaa477cc0992;hb=bd23b35be6c105946c9ecf87785f409f69a24714;hpb=0fcfaff680c6974a9b3f2a63704a8eb788d3fe94 diff --git a/bs/planet.cpp b/bs/planet.cpp index 03c4537..c374a99 100644 --- a/bs/planet.cpp +++ b/bs/planet.cpp @@ -17,7 +17,225 @@ #include "planet.h" -Planet::Planet(){ +#include +using namespace std; + +Planet::Planet() +{ + m_sRace = "Planet"; + m_iScore = 0; } + Planet::~Planet(){ } + +////////////////////////////////////////////////////////////////////////// +// +unsigned Planet::planetScore() const +{ + return m_iScore; +} + +////////////////////////////////////////////////////////////////////////// +// +void Planet::setPlanetScore(unsigned i) +{ + m_iScore = i; +} + +////////////////////////////////////////////////////////////////////////// +// +int Planet::roids(std::string type, int tick = 0) const +{ + // const... I would like [] as for const types: int ticks = m_Roids[type].size(); + + vectorconst* roids = 0; + for (RoidList::const_iterator i = m_Roids.begin(); i != m_Roids.end(); ++i) + { + if (i->first == type) + { + roids = &i->second; + break; + } + } + if (roids == 0) + return 0; + + int ticks = roids->size(); + + if( ticks == 0) + return 0; + --ticks; + if (ticks < tick) + return roids->at(ticks); + return roids->at(tick); +} + +////////////////////////////////////////////////////////////////////////// +// +void Planet::setRoids(std::string type, int number, int tick = 0) +{ + int ticks = m_Roids[type].size(); + int roids = 0; + if (m_Roids[type].size() > 0) + roids = m_Roids[type][m_Roids[type].size() - 1]; + + for (int i = ticks; i <= tick; ++i ) + m_Roids[type].push_back(roids); + m_Roids[type][tick] = number; +} + +////////////////////////////////////////////////////////////////////////// +// +void Planet::takeRoids(std::string type, int number, int tick = 0) +{ + int ticks = m_Roids[type].size(); + int roids = 0; + if (m_Roids[type].size() > 0) + roids = m_Roids[type][m_Roids[type].size() - 1]; + + for (int i = ticks; i <= tick; ++i ) + m_Roids[type].push_back(roids); + m_Roids[type][tick] -= number; +} + + + +////////////////////////////////////////////////////////////////////////// +// +void Planet::runBattle(std::vector friendly, std::vector hostile) +{ + if (hostile.size() == 0) + return; + + int skipped = 0; + for(int tick = 1; skipped < 20; ++tick) + { + //See who's in the battle at the current tick + vector friends = calculateSide(friendly, 6, tick); + vector hostiles = calculateSide(hostile, 3, tick); + + // No idea to calculate anything if noone is there.. ;) + if (hostiles.size() == 0) + { + skipped++; + continue; + } + else + skipped = 0; + + Planet allFriends; + allFriends.addToThis(friends, tick - 1); + + /*for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i) + allFriends.setRoids(i->first, roids(i->first, tick)); + + allFriends.setPlanetScore(m_iScore);*/ + + Fleet allHostiles; + allHostiles.addToThis(hostiles, tick - 1); + + map > stealfriendly; + map > stealhostile; + + calcOneTick(&allFriends, &allHostiles, stealfriendly, stealhostile, tick ); + + //allFriends.printFleet(); + + allFriends.distributeLossesGains(friends, tick); + allHostiles.distributeLossesGains(hostiles, tick); + allHostiles.distributeCappedRoids(hostiles, tick); + + /* for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i) + setRoids(i->first, roids(i->first, 1), tick);*/ + } + + for (RoidList::iterator i = m_Roids.begin(); i != m_Roids.end(); ++i) + { + for (vector::iterator j = i->second.begin(); j != i->second.end(); ++j) + cout << i->first << " roids : " << (*j) << endl; + } +} + +////////////////////////////////////////////////////////////////////////// +// +void Planet::calcOneTick(Planet* friendly, Fleet* hostile, std::map >& stealfriendly, std::map >& stealhostile, int tick = 0) +{ + map unitsinit; // order units after their ininitiative + for (UnitList::iterator i = s_Units.begin(); i != s_Units.end(); ++i) + unitsinit[i->second.initiative()] = i->first; + + for (map::iterator i = unitsinit.begin(); i != unitsinit.end(); ++i) + { + Fleet* hostiletemp = new Fleet(*hostile); + Planet* friendlytemp = new Planet(*friendly); + + string unittype = i->second; + + //cerr << "Initiative: " << s_Units[unittype].initiative() << " with unit: " << unittype << endl; + + if (s_Units[unittype].type() == "EMP") + { + hostiletemp->takeEMP(unittype, friendly->freeFleet(unittype, 1)); + friendlytemp->takeEMP(unittype, hostile->freeFleet(unittype, 1)); + } + else if (s_Units[unittype].type() == "Steal") + { + hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), stealfriendly[unittype]); + friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), stealhostile[unittype]); + } + else + { + map temp; + hostiletemp->takeShoot(unittype, friendly->freeFleet(unittype, 1), temp); + friendlytemp->takeShoot(unittype, hostile->freeFleet(unittype, 1), temp); + } + + if (s_Units[unittype].type() == "Pod") + { + if (m_iScore > 0) + { + float capping = float(m_iScore) / hostile->score() / 10; + + cerr << "Capping is: " << capping << endl; + + if (capping > 0.15) + capping = 0.15; + if (capping > 0) + { + for (RoidList::iterator roid = m_Roids.begin(); roid != m_Roids.end(); ++roid) + { + int caproids = capping * roids(roid->first, tick - 1); + int freepods = hostiletemp->freeFleet(unittype, 1); + + cerr << "Possible to steal " << caproids << " " << roid->first << " roids\n"; + cerr << freepods << " free pods available\n"; + + if (freepods <= 0) + break; + if (freepods < caproids) + caproids = freepods; + + cerr << caproids << " roids stolen\n"; + + takeRoids(roid->first, caproids, tick); + + //FIXME: Going to move this to the distribute roids section instead.. + hostiletemp->killFleet(unittype, caproids, 1); + //int totroids = caproids + hostiletemp->resource(roid->first, 0); + hostiletemp->addResource(roid->first, caproids, 1); + + cerr << caproids << " stolen " << roid->first << " roids\n"; + } + } + } + } + + //set the the objects so they point at the modified objects + *friendly = *friendlytemp; + delete friendlytemp; + *hostile = *hostiletemp; + delete hostiletemp; + } +} +