int last_bucket; /**< Last bucket acted upon. */ #endregion Fields #region Constructors public MapContent(Map map) { last_bucket=0; zones=null; buckets[0]=new ObjectBucket(); buckets[0].allocate(); // Skip ID 0 for(int i=1; i<256; ++i) //TODO Unötig in C#? { buckets[i]=null; } mapWidth=(ushort)((map.getWidth()*map.getTileWidth()+zoneDiam-1) /zoneDiam); mapHeight=(ushort)((map.getHeight()*map.getTileHeight()+zoneDiam-1) /zoneDiam); zones=new MapZone[mapWidth*mapHeight]; //ZOnen initialisieren //TODO wird das an anderer Stelle getan? for(int i=0; i<zones.Length; i++) { zones[i]=new MapZone(); } things=new List<Thing>(); }
//MapContent::~MapContent() //{ // for (int i = 0; i < 256; ++i) // { // delete buckets[i]; // } // delete[] zones; //} public bool allocate(Actor obj) { // First, try allocating from the last used bucket. ObjectBucket b=buckets[last_bucket]; int i=b.allocate(); if(i>=0) { b.objects[i]=obj; obj.setPublicID(last_bucket*256+i); return true; } /* If the last used bucket is already full, scan all the buckets for an empty place. If none is available, create a new bucket. */ for(i=0; i<256; ++i) { b=buckets[i]; if(b!=null) { /* Buckets are created in order. If there is nothing at position i, there will not be anything in the next positions. So create a new bucket. */ b=new ObjectBucket(); buckets[i]=b; Logger.Write(LogLevel.Debug, "New bucket created"); } int j=b.allocate(); if(j>=0) { last_bucket=i; b.objects[j]=obj; obj.setPublicID(last_bucket*256+j); return true; } } // All the IDs are currently used, fail. Logger.Write(LogLevel.Error, "unable to allocate id"); return false; }