Browse Source

faster entId allocation

Darren Ranalli 22 years ago
parent
commit
4927912722
1 changed files with 25 additions and 8 deletions
  1. 25 8
      direct/src/level/EditMgrAI.py

+ 25 - 8
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