From 4ce27a2dc501949c30866ecb077b194596aad240 Mon Sep 17 00:00:00 2001 From: darkrose Date: Thu, 25 Apr 2013 13:24:43 +1000 Subject: [PATCH] various cleanups and optimisations --- src/environment.cpp | 37 +++++++++++++++++++++---------------- src/inventory.cpp | 18 +++++++----------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/environment.cpp b/src/environment.cpp index 776e1c7..42a1e85 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1439,17 +1439,20 @@ bool isFreeServerActiveObjectId(u16 id, u16 getFreeServerActiveObjectId( core::map &objects) { - u16 new_id = 1; - for(;;) - { - if(isFreeServerActiveObjectId(new_id, objects)) - return new_id; + //try to reuse id's as late as possible + static u16 last_used_id = 0; + u16 startid = last_used_id; - if(new_id == 65535) + for (;;) { + last_used_id++; + if (isFreeServerActiveObjectId(last_used_id, objects)) + return last_used_id; + + if (last_used_id == startid) return 0; - - new_id++; } + + return 0; } u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) @@ -2342,17 +2345,19 @@ bool isFreeClientActiveObjectId(u16 id, u16 getFreeClientActiveObjectId( core::map &objects) { - u16 new_id = 1; - for(;;) - { - if(isFreeClientActiveObjectId(new_id, objects)) - return new_id; + //try to reuse id's as late as possible + static u16 last_used_id = 0; + u16 startid = last_used_id; - if(new_id == 65535) + for (;;) { + last_used_id ++; + if (isFreeClientActiveObjectId(last_used_id, objects)) + return last_used_id; + + if(last_used_id == startid) return 0; - - new_id++; } + return 0; } u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) diff --git a/src/inventory.cpp b/src/inventory.cpp index ecdc10a..db25cf9 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -712,9 +712,8 @@ const InventoryList * Inventory::getList(const std::string &name) const const s32 Inventory::getListIndex(const std::string &name) const { - for(u32 i=0; igetName() == name) + for(u32 i=0; igetName() == name) return i; } return -1; @@ -729,22 +728,19 @@ InventoryAction * InventoryAction::deSerialize(std::istream &is) std::string type; std::getline(is, type, ' '); - InventoryAction *a = NULL; - if(type == "Move") - { - a = new IMoveAction(is); - } + return new IMoveAction(is); - return a; + return NULL; } static std::string describeC(const struct InventoryContext *c) { - if(c->current_player == NULL) + if(c->current_player == NULL) { return "current_player=NULL"; - else + }else{ return std::string("current_player=") + c->current_player->getName(); + } } void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)