LBM: use range based for and fixed a loop variable overloading in applyLBMs

This commit is contained in:
Loic Blot 2017-07-24 08:26:19 +02:00
parent dc9e4517a8
commit befc3bba3d
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
1 changed files with 24 additions and 33 deletions

View File

@ -69,9 +69,8 @@ ABMWithState::ABMWithState(ActiveBlockModifier *abm_):
void LBMContentMapping::deleteContents() void LBMContentMapping::deleteContents()
{ {
for (std::vector<LoadingBlockModifierDef *>::iterator it = lbm_list.begin(); for (auto &it : lbm_list) {
it != lbm_list.end(); ++it) { delete it;
delete *it;
} }
} }
@ -83,24 +82,21 @@ void LBMContentMapping::addLBM(LoadingBlockModifierDef *lbm_def, IGameDef *gamed
lbm_list.push_back(lbm_def); lbm_list.push_back(lbm_def);
for (std::set<std::string>::const_iterator it = lbm_def->trigger_contents.begin(); for (const std::string &nodeTrigger: lbm_def->trigger_contents) {
it != lbm_def->trigger_contents.end(); ++it) {
std::set<content_t> c_ids; std::set<content_t> c_ids;
bool found = nodedef->getIds(*it, c_ids); bool found = nodedef->getIds(nodeTrigger, c_ids);
if (!found) { if (!found) {
content_t c_id = gamedef->allocateUnknownNodeId(*it); content_t c_id = gamedef->allocateUnknownNodeId(nodeTrigger);
if (c_id == CONTENT_IGNORE) { if (c_id == CONTENT_IGNORE) {
// Seems it can't be allocated. // Seems it can't be allocated.
warningstream << "Could not internalize node name \"" << *it warningstream << "Could not internalize node name \"" << nodeTrigger
<< "\" while loading LBM \"" << lbm_def->name << "\"." << std::endl; << "\" while loading LBM \"" << lbm_def->name << "\"." << std::endl;
continue; continue;
} }
c_ids.insert(c_id); c_ids.insert(c_id);
} }
for (std::set<content_t>::const_iterator iit = for (content_t c_id : c_ids) {
c_ids.begin(); iit != c_ids.end(); ++iit) {
content_t c_id = *iit;
map[c_id].push_back(lbm_def); map[c_id].push_back(lbm_def);
} }
} }
@ -120,13 +116,12 @@ LBMContentMapping::lookup(content_t c) const
LBMManager::~LBMManager() LBMManager::~LBMManager()
{ {
for (std::map<std::string, LoadingBlockModifierDef *>::iterator it = for (auto &m_lbm_def : m_lbm_defs) {
m_lbm_defs.begin(); it != m_lbm_defs.end(); ++it) { delete m_lbm_def.second;
delete it->second;
} }
for (lbm_lookup_map::iterator it = m_lbm_lookup.begin();
it != m_lbm_lookup.end(); ++it) { for (auto &it : m_lbm_lookup) {
(it->second).deleteContents(); (it.second).deleteContents();
} }
} }
@ -212,12 +207,11 @@ void LBMManager::loadIntroductionTimes(const std::string &times,
LBMContentMapping &lbms_we_introduce_now = m_lbm_lookup[now]; LBMContentMapping &lbms_we_introduce_now = m_lbm_lookup[now];
LBMContentMapping &lbms_running_always = m_lbm_lookup[U32_MAX]; LBMContentMapping &lbms_running_always = m_lbm_lookup[U32_MAX];
for (std::map<std::string, LoadingBlockModifierDef *>::iterator it = for (auto &m_lbm_def : m_lbm_defs) {
m_lbm_defs.begin(); it != m_lbm_defs.end(); ++it) { if (m_lbm_def.second->run_at_every_load) {
if (it->second->run_at_every_load) { lbms_running_always.addLBM(m_lbm_def.second, gamedef);
lbms_running_always.addLBM(it->second, gamedef);
} else { } else {
lbms_we_introduce_now.addLBM(it->second, gamedef); lbms_we_introduce_now.addLBM(m_lbm_def.second, gamedef);
} }
} }
@ -233,18 +227,16 @@ std::string LBMManager::createIntroductionTimesString()
"attempted to query on non fully set up LBMManager"); "attempted to query on non fully set up LBMManager");
std::ostringstream oss; std::ostringstream oss;
for (lbm_lookup_map::iterator it = m_lbm_lookup.begin(); for (const auto &it : m_lbm_lookup) {
it != m_lbm_lookup.end(); ++it) { u32 time = it.first;
u32 time = it->first; const std::vector<LoadingBlockModifierDef *> &lbm_list = it.second.lbm_list;
std::vector<LoadingBlockModifierDef *> &lbm_list = it->second.lbm_list; for (const auto &lbm_def : lbm_list) {
for (std::vector<LoadingBlockModifierDef *>::iterator iit = lbm_list.begin();
iit != lbm_list.end(); ++iit) {
// Don't add if the LBM runs at every load, // Don't add if the LBM runs at every load,
// then introducement time is hardcoded // then introducement time is hardcoded
// and doesn't need to be stored // and doesn't need to be stored
if ((*iit)->run_at_every_load) if (lbm_def->run_at_every_load)
continue; continue;
oss << (*iit)->name << "~" << time << ";"; oss << lbm_def->name << "~" << time << ";";
} }
} }
return oss.str(); return oss.str();
@ -272,9 +264,8 @@ void LBMManager::applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp)
iit->second.lookup(c); iit->second.lookup(c);
if (!lbm_list) if (!lbm_list)
continue; continue;
for (std::vector<LoadingBlockModifierDef *>::const_iterator iit = for (auto lbmdef : *lbm_list) {
lbm_list->begin(); iit != lbm_list->end(); ++iit) { lbmdef->trigger(env, pos + pos_of_block, n);
(*iit)->trigger(env, pos + pos_of_block, n);
} }
} }
} }