BWAPI
|
#include <ResourceTracker.h>
Public Member Functions | |
ResourceTrackerClass () | |
void | reservePlannedMinerals (int time, int amount) |
void | reservePlannedGas (int time, int amount) |
void | reservePlannedSupply (int time, int amount) |
void | reset () |
void | reserveCurrentMinerals (int time, int amount) |
void | reserveCurrentGas (int time, int amount) |
void | reserveCurrentSupply (int time, int amount) |
void | releaseCurrentMinerals (int time, int amount) |
void | releaseCurrentGas (int time, int amount) |
void | releaseCurrentSupply (int time, int amount) |
int | earliestMineralAvailability (int amount) |
int | earliestGasAvailability (int amount) |
int | earliestSupplyAvailability (int amount) |
int | availableMineralAtTime (int time) |
int | availableGasAtTime (int time) |
int | availableSupplyAtTime (int time) |
int | totalMineralAtTime (int time) |
int | totalGasAtTime (int time) |
int | totalSupplyAtTime (int time) |
double | getMineralRate () |
double | getGasRate () |
void | setMineralRate (double rate) |
void | setGasRate (double rate) |
Private Attributes | |
std::map< int, int > | mSupplyTime |
std::map< int, int > | mFreeSupply |
std::map< int, int > | mPlannedReservedMinerals |
std::map< int, int > | mPlannedReservedGas |
std::map< int, int > | mPlannedReservedSupply |
std::map< int, int > | mCurrentReservedMinerals |
std::map< int, int > | mCurrentReservedGas |
std::map< int, int > | mCurrentReservedSupply |
double | mMineralRate |
double | mGasRate |
Definition at line 7 of file ResourceTracker.h.
Definition at line 10 of file ResourceTracker.cpp.
{ }
int ResourceTrackerClass::availableGasAtTime | ( | int | time | ) |
Definition at line 345 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Player::gas(), BWAPI::Game::getFrameCount(), mGasRate, mPlannedReservedGas, and BWAPI::Game::self().
{ double freeGas = BWAPI::Broodwar->self()->gas(); int lastTime = BWAPI::Broodwar->getFrameCount(); for(std::map<int, int>::iterator i = mPlannedReservedGas.begin(); i != mPlannedReservedGas.end(); ++i) { if(i->first > time) break; freeGas -= i->second; if(i->first >= lastTime) { int timePassed = i->first - lastTime; lastTime = i->first; freeGas += timePassed * mGasRate; } } if(time > lastTime) { int timePassed = time - lastTime; freeGas += timePassed * mGasRate; } return int(freeGas); }
int ResourceTrackerClass::availableMineralAtTime | ( | int | time | ) |
Definition at line 316 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Game::getFrameCount(), BWAPI::Player::minerals(), mMineralRate, mPlannedReservedMinerals, and BWAPI::Game::self().
{ double freeMinerals = BWAPI::Broodwar->self()->minerals(); int lastTime = BWAPI::Broodwar->getFrameCount(); for(std::map<int, int>::iterator i = mPlannedReservedMinerals.begin(); i != mPlannedReservedMinerals.end(); ++i) { if(i->first > time) break; freeMinerals -= i->second; if(i->first >= lastTime) { int timePassed = i->first - lastTime; lastTime = i->first; freeMinerals += timePassed * mMineralRate; } } if(time > lastTime) { int timePassed = time - lastTime; freeMinerals += timePassed * mMineralRate; } return int(freeMinerals); }
int ResourceTrackerClass::availableSupplyAtTime | ( | int | time | ) |
Definition at line 374 of file ResourceTracker.cpp.
References BWAPI::Broodwar, mPlannedReservedSupply, BWAPI::Game::self(), BWAPI::Player::supplyUsed(), and totalSupplyAtTime().
{ int total = totalSupplyAtTime(time); for(std::map<int, int>::iterator i = mPlannedReservedSupply.begin(); i != mPlannedReservedSupply.end(); ++i) { if(i->first > time) break; else total -= i->second; } total -= BWAPI::Broodwar->self()->supplyUsed(); return total; }
int ResourceTrackerClass::earliestGasAvailability | ( | int | amount | ) |
Definition at line 251 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Player::gas(), BWAPI::Game::getFrameCount(), Requirement::maxTime, mGasRate, mPlannedReservedGas, and BWAPI::Game::self().
{ double freeGas = BWAPI::Broodwar->self()->gas(); bool hasSpace = false; if(freeGas >= amount) { freeGas -= amount; hasSpace = true; } int earliestTime = BWAPI::Broodwar->getFrameCount(); int lastTime = BWAPI::Broodwar->getFrameCount(); for(std::map<int, int>::iterator i = mPlannedReservedGas.begin(); i != mPlannedReservedGas.end(); ++i) { freeGas -= i->second; if(i->first >= lastTime) { int timePassed = i->first - lastTime; lastTime = i->first; freeGas += timePassed * mGasRate; } if(!hasSpace && freeGas >= amount) { if(mGasRate == 0) return Requirement::maxTime; freeGas -= amount; hasSpace = true; earliestTime = int(i->first - (freeGas / mGasRate)); } else if(freeGas < 0 && hasSpace) { hasSpace = false; freeGas += amount; } } if(!hasSpace) { if(mGasRate == 0) return Requirement::maxTime; freeGas -= amount; earliestTime = lastTime + int(-freeGas / mGasRate); } return earliestTime; }
int ResourceTrackerClass::earliestMineralAvailability | ( | int | amount | ) |
Definition at line 199 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Game::getFrameCount(), Requirement::maxTime, BWAPI::Player::minerals(), mMineralRate, mPlannedReservedMinerals, and BWAPI::Game::self().
{ double freeMinerals = BWAPI::Broodwar->self()->minerals(); bool hasSpace = false; if(freeMinerals >= amount) { freeMinerals -= amount; hasSpace = true; } int earliestTime = BWAPI::Broodwar->getFrameCount(); int lastTime = BWAPI::Broodwar->getFrameCount(); for(std::map<int, int>::iterator i = mPlannedReservedMinerals.begin(); i != mPlannedReservedMinerals.end(); ++i) { freeMinerals -= i->second; if(i->first >= lastTime) { int timePassed = i->first - lastTime; lastTime = i->first; freeMinerals += timePassed * mMineralRate; } if(!hasSpace && freeMinerals >= amount) { if(mMineralRate == 0) return Requirement::maxTime; freeMinerals -= amount; hasSpace = true; earliestTime = int(i->first - (freeMinerals / mMineralRate)); } else if(freeMinerals < 0 && hasSpace) { hasSpace = false; freeMinerals += amount; } } if(!hasSpace) { if(mMineralRate == 0) return Requirement::maxTime; freeMinerals -= amount; earliestTime = lastTime + int(-freeMinerals / mMineralRate); } return earliestTime; }
int ResourceTrackerClass::earliestSupplyAvailability | ( | int | amount | ) |
Definition at line 303 of file ResourceTracker.cpp.
References Requirement::maxTime, and mFreeSupply.
{ int supplyTotal = 0; for(std::map<int, int>::iterator i = mFreeSupply.begin(); i != mFreeSupply.end(); ++i) { supplyTotal += i->second; if(supplyTotal >= amount) return i->first; } return Requirement::maxTime; }
double ResourceTrackerClass::getGasRate | ( | ) | [inline] |
double ResourceTrackerClass::getMineralRate | ( | ) | [inline] |
void ResourceTrackerClass::releaseCurrentGas | ( | int | time, |
int | amount | ||
) |
Definition at line 189 of file ResourceTracker.cpp.
References mCurrentReservedGas.
{ mCurrentReservedGas[time] -= amount; }
void ResourceTrackerClass::releaseCurrentMinerals | ( | int | time, |
int | amount | ||
) |
Definition at line 184 of file ResourceTracker.cpp.
References mCurrentReservedMinerals.
{ mCurrentReservedMinerals[time] -= amount; }
void ResourceTrackerClass::releaseCurrentSupply | ( | int | time, |
int | amount | ||
) |
Definition at line 194 of file ResourceTracker.cpp.
References mCurrentReservedSupply.
{ mCurrentReservedSupply[time] -= amount; }
void ResourceTrackerClass::reserveCurrentGas | ( | int | time, |
int | amount | ||
) |
Definition at line 166 of file ResourceTracker.cpp.
References Requirement::maxTime, mCurrentReservedGas, and reservePlannedGas().
{ if(time == Requirement::maxTime || amount == 0) return; mCurrentReservedGas[time] += amount; reservePlannedGas(time, amount); }
void ResourceTrackerClass::reserveCurrentMinerals | ( | int | time, |
int | amount | ||
) |
Definition at line 157 of file ResourceTracker.cpp.
References Requirement::maxTime, mCurrentReservedMinerals, and reservePlannedMinerals().
{ if(time == Requirement::maxTime || amount == 0) return; mCurrentReservedMinerals[time] += amount; reservePlannedMinerals(time, amount); }
void ResourceTrackerClass::reserveCurrentSupply | ( | int | time, |
int | amount | ||
) |
Definition at line 175 of file ResourceTracker.cpp.
References Requirement::maxTime, mCurrentReservedSupply, and reservePlannedSupply().
{ if(time == Requirement::maxTime || amount == 0) return; mCurrentReservedSupply[time] += amount; reservePlannedSupply(time, amount); }
void ResourceTrackerClass::reservePlannedGas | ( | int | time, |
int | amount | ||
) |
Definition at line 22 of file ResourceTracker.cpp.
References Requirement::maxTime, and mPlannedReservedGas.
Referenced by reserveCurrentGas().
{ if(time == Requirement::maxTime || amount == 0) return; mPlannedReservedGas[time] += amount; }
void ResourceTrackerClass::reservePlannedMinerals | ( | int | time, |
int | amount | ||
) |
Definition at line 14 of file ResourceTracker.cpp.
References Requirement::maxTime, and mPlannedReservedMinerals.
Referenced by reserveCurrentMinerals().
{ if(time == Requirement::maxTime || amount == 0) return; mPlannedReservedMinerals[time] += amount; }
void ResourceTrackerClass::reservePlannedSupply | ( | int | time, |
int | amount | ||
) |
Definition at line 30 of file ResourceTracker.cpp.
References LOGMESSAGEWARNING, Requirement::maxTime, mFreeSupply, and mPlannedReservedSupply.
Referenced by reserveCurrentSupply(), and reset().
{ if(time == Requirement::maxTime || amount == 0) return; mPlannedReservedSupply[time] += amount; for(std::map<int, int>::reverse_iterator i = mFreeSupply.rbegin(); i != mFreeSupply.rend(); ++i) { if(i->first <= time) { i->second -= amount; if(i->second <= 0) { amount = -i->second; i->second = 0; if(amount == 0) return; } else return; } } for(std::map<int, int>::iterator i = mFreeSupply.begin(); i != mFreeSupply.end(); ++i) { i->second -= amount; if(i->second <= 0) { amount = -i->second; i->second = 0; if(amount == 0) return; } else return; } LOGMESSAGEWARNING("It tried to reserve supply it didn't have"); }
void ResourceTrackerClass::reset | ( | ) |
Definition at line 72 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Game::getFrameCount(), Singleton< T >::Instance(), mCurrentReservedGas, mCurrentReservedMinerals, mCurrentReservedSupply, mFreeSupply, mPlannedReservedGas, mPlannedReservedMinerals, mPlannedReservedSupply, mSupplyTime, reservePlannedSupply(), BWAPI::Game::self(), and BWAPI::Player::supplyUsed().
{ mPlannedReservedMinerals.clear(); mPlannedReservedGas.clear(); mPlannedReservedSupply.clear(); mFreeSupply.clear(); mSupplyTime.clear(); int timeNow = BWAPI::Broodwar->getFrameCount(); for(std::map<int, int>::iterator it = mCurrentReservedMinerals.begin(); it != mCurrentReservedMinerals.end();) { if(it->second <= 0) mCurrentReservedMinerals.erase(it++); else ++it; } mPlannedReservedMinerals = mCurrentReservedMinerals; for(std::map<int, int>::iterator it = mCurrentReservedGas.begin(); it != mCurrentReservedGas.end();) { if(it->second <= 0) mCurrentReservedGas.erase(it++); else ++it; } mPlannedReservedGas = mCurrentReservedGas; // Calculate free supply we have in the future for each(Unit unit in UnitTracker::Instance().getSupplyProviders())//TODO: hatcheries and lairs morphing return false but still give supply { if(unit->isCompleted()) mFreeSupply[timeNow] += unit->getType().supplyProvided(); else mFreeSupply[unit->getCompletedTime()] += unit->getType().supplyProvided(); } // Cap it at 400 int supplyTotal = 0; for(std::map<int, int>::iterator it = mFreeSupply.begin(); it != mFreeSupply.end(); ++it) { supplyTotal += it->second; if(supplyTotal > 400) { int difference = supplyTotal - 400; it->second -= difference; supplyTotal = 400; } } mSupplyTime = mFreeSupply; // Remove the supply we are already using and that we plan to use int supplyUsed = BWAPI::Broodwar->self()->supplyUsed(); supplyTotal = 0; for(std::map<int, int>::iterator it = mFreeSupply.begin(); it != mFreeSupply.end();) { if(supplyUsed > 0) { int supplyToUse = std::min(supplyUsed, it->second); it->second -= supplyToUse; supplyUsed -= supplyToUse; } supplyTotal += it->second; if(it->second == 0) mFreeSupply.erase(it++); else ++it; } for(std::map<int, int>::iterator i = mCurrentReservedSupply.begin(); i != mCurrentReservedSupply.end();) { if(i->second > 0) { int supplyToTake = std::min(supplyTotal, i->second); supplyTotal -= supplyToTake; reservePlannedSupply(i->first, supplyToTake); ++i; } else mCurrentReservedSupply.erase(i++); } }
void ResourceTrackerClass::setGasRate | ( | double | rate | ) | [inline] |
void ResourceTrackerClass::setMineralRate | ( | double | rate | ) | [inline] |
int ResourceTrackerClass::totalGasAtTime | ( | int | time | ) |
Definition at line 396 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Player::gas(), mGasRate, and BWAPI::Game::self().
{ return BWAPI::Broodwar->self()->gas() + int((time - BWAPI::Broodwar->getFrameCount()) * mGasRate); }
int ResourceTrackerClass::totalMineralAtTime | ( | int | time | ) |
Definition at line 391 of file ResourceTracker.cpp.
References BWAPI::Broodwar, BWAPI::Player::minerals(), mMineralRate, and BWAPI::Game::self().
{ return BWAPI::Broodwar->self()->minerals() + int((time - BWAPI::Broodwar->getFrameCount()) * mMineralRate); }
int ResourceTrackerClass::totalSupplyAtTime | ( | int | time | ) |
Definition at line 401 of file ResourceTracker.cpp.
References mSupplyTime.
Referenced by availableSupplyAtTime().
{ int total = 0; for(std::map<int, int>::iterator i = mSupplyTime.begin(); i != mSupplyTime.end(); ++i) { if(i->first > time) break; else total += i->second; } return total; }
std::map<int, int> ResourceTrackerClass::mCurrentReservedGas [private] |
Definition at line 52 of file ResourceTracker.h.
Referenced by releaseCurrentGas(), reserveCurrentGas(), and reset().
std::map<int, int> ResourceTrackerClass::mCurrentReservedMinerals [private] |
Definition at line 51 of file ResourceTracker.h.
Referenced by releaseCurrentMinerals(), reserveCurrentMinerals(), and reset().
std::map<int, int> ResourceTrackerClass::mCurrentReservedSupply [private] |
Definition at line 53 of file ResourceTracker.h.
Referenced by releaseCurrentSupply(), reserveCurrentSupply(), and reset().
std::map<int, int> ResourceTrackerClass::mFreeSupply [private] |
Definition at line 44 of file ResourceTracker.h.
Referenced by earliestSupplyAvailability(), reservePlannedSupply(), and reset().
double ResourceTrackerClass::mGasRate [private] |
Definition at line 56 of file ResourceTracker.h.
Referenced by availableGasAtTime(), earliestGasAvailability(), getGasRate(), setGasRate(), and totalGasAtTime().
double ResourceTrackerClass::mMineralRate [private] |
Definition at line 55 of file ResourceTracker.h.
Referenced by availableMineralAtTime(), earliestMineralAvailability(), getMineralRate(), setMineralRate(), and totalMineralAtTime().
std::map<int, int> ResourceTrackerClass::mPlannedReservedGas [private] |
Definition at line 48 of file ResourceTracker.h.
Referenced by availableGasAtTime(), earliestGasAvailability(), reservePlannedGas(), and reset().
std::map<int, int> ResourceTrackerClass::mPlannedReservedMinerals [private] |
Definition at line 47 of file ResourceTracker.h.
Referenced by availableMineralAtTime(), earliestMineralAvailability(), reservePlannedMinerals(), and reset().
std::map<int, int> ResourceTrackerClass::mPlannedReservedSupply [private] |
Definition at line 49 of file ResourceTracker.h.
Referenced by availableSupplyAtTime(), reservePlannedSupply(), and reset().
std::map<int, int> ResourceTrackerClass::mSupplyTime [private] |
Definition at line 43 of file ResourceTracker.h.
Referenced by reset(), and totalSupplyAtTime().