From 4927912722dbb13cb1535424295823f2a2074da8 Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Tue, 27 Jan 2004 00:54:31 +0000 Subject: [PATCH] faster entId allocation --- direct/src/level/EditMgrAI.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/direct/src/level/EditMgrAI.py b/direct/src/level/EditMgrAI.py index f2a2aa6daf..0202ee551a 100755 --- a/direct/src/level/EditMgrAI.py +++ b/direct/src/level/EditMgrAI.py @@ -1,7 +1,7 @@ """EditMgrAI module: contains the EditMgrAI class""" import EditMgrBase -if __debug__: +if __dev__: from PythonUtil import list2dict import EditorGlobals @@ -14,20 +14,37 @@ class EditMgrAI(EditMgrBase.EditMgrBase): entIds = spec.getAllEntIds() entIdDict = list2dict(entIds) - # dumb linear search for now - # TODO: make this smarter (cache last-allocated id) # Note that this uses the ID range associated with the # AI's username, not the username of the user who requested # the new entity. - for id in xrange(*EditorGlobals.getEntIdAllocRange()): - if not id in entIdDict: - break - else: - self.notify.error('out of entIds') + allocRange = EditorGlobals.getEntIdAllocRange() + + if not hasattr(self, 'lastAllocatedEntId'): + self.lastAllocatedEntId = allocRange[0] + + idChosen = 0 + while not idChosen: + # linear search for an unused entId starting with the + # last-allocated id + for id in xrange(self.lastAllocatedEntId, allocRange[1]): + print id + if not id in entIdDict: + idChosen = 1 + break + else: + # we ran off the end of the range. + if self.lastAllocatedEntId != allocRange[0]: + # if we started in the middle, try again from + # the beginning + self.lastAllocatedEntId = allocRange[0] + else: + # every entId is used!! + self.notify.error('out of entIds') # OK, we've chosen an unused entId. Add the entId to the data # dict and do the insert data.update({'entId': id}) + self.lastAllocatedEntId = id self.level.setAttribChange(self.entId, 'insertEntity', data) # clear out the attrib, it shouldn't be kept in the spec