/* It is assumed that a game will have many different types of Bots, thinking in different ways, some of them even custom bots defined by the game app rather than by this framework. So, in order to make these bots recyclable, we have to register them with this recycler so that this recycler knows how to make new ones as needed. */ void BotRecycler::registerBotType(wstring botType, Bot *firstBot) { // FIRST MAKE SURE WE DON'T ALREADY HAVE A BUNCH OF RECYCLABLE // BOTS FOR THIS TYPE. TO DO THIS, WE USE SOME C++ WEIRDNESS. if (!isRegisteredBotType(botType)) { // REGISTER THE BOT registeredBotTypes[botType] = firstBot; recyclableBots[botType] = new list<Bot*>(); addMoreBots(botType, RECYCLABLE_BOT_INCREMENT - 1); } }
void BotRecycler::initRecyclableBots(wstring botType, unsigned int maxBotsNeeded) { // FIRST MAKE SURE WE DON'T ALREADY HAVE A BUNCH OF RECYCLABLE // BOTS FOR THIS TYPE. TO DO THIS, WE USE SOME C++ WEIRDNESS. map<wstring, list<Bot *> *>::iterator testIt = recyclableBots.find(botType); if (testIt == recyclableBots.end()) { // MAKE THE RECYCLE LIST recyclableBots[botType] = new list<Bot *>(); // AND ADD THE BOTS addMoreBots(recyclableBots[botType], botType, maxBotsNeeded); } }
Bot* BotRecycler::retrieveBot(wstring botType) { // FIRST MAKE SURE THIS IS A REGISTERED BOT TYPE, // IF IT IS NOT, WE NEED TO RETURN NULL if (!isRegisteredBotType(botType)) { return nullptr; } else { // GET THE CORRECT LIST OF BOT TYPES list<Bot *> *botsOfTypeWeNeed = recyclableBots[botType]; // MAKE SURE WE ARE NOT OUT OF THIS TYPE OF BOT if(botsOfTypeWeNeed->size() == 0) addMoreBots(botType, RECYCLABLE_BOT_INCREMENT); // NOW GET THE LAST ELEMENT Bot* botToReturn = botsOfTypeWeNeed->back(); botsOfTypeWeNeed->pop_back(); return botToReturn; } }