Giter Site home page Giter Site logo

Comments (4)

geoffthemedio avatar geoffthemedio commented on May 9, 2024

As is, this doesn't make much sense to me either...

But I think it's supposed to be scanning through the ids, looking for one that isn't being used. That would occur if the gap between two successive ids in the ObjectMap was greater than 1. For example, if it saw ids 10, 11, 12, 15, 16, 17. It's trying to find cases like the gap between 12 and 15, as then it can take the last seen id (12) and add 1 to get a new unused id (13).

I took a look through some old versions, but didn't find when this was added or changed. Much earlier, there was no second-try scanning at all.

from freeorion.

Vezzra avatar Vezzra commented on May 9, 2024

Provided the ObjectMap is ordered by ID (which I think it is), to achieve the behavior you describe I think all that's needed is an additional line within the loop, after the if block, that sets last_id_seen to it->ID(), so in case the if statement doesn't evaluate to true last_id_seen gets properly updated to the last id seen. I guess whoever wrote that code just forgot that line?

So the function would look like this:

int Universe::GenerateObjectID() {
    if (m_last_allocated_object_id + 1 < MAX_ID)
        return ++m_last_allocated_object_id;

    int last_id_seen = -1; // 0 is the first valid object id
    for (ObjectMap::iterator<> it = m_objects.begin(); it != m_objects.end(); ++it) {
        if (it->ID() - last_id_seen > 1)
            return last_id_seen + 1;
        last_id_seen = it->ID();
    }

    return INVALID_OBJECT_ID; // We're screwed.
}

I guess that should work?

from freeorion.

geoffthemedio avatar geoffthemedio commented on May 9, 2024

should probably put some comments in there to explain it...

from freeorion.

Vezzra avatar Vezzra commented on May 9, 2024

I can't get that to work. Although I managed to get this function to correctly yield unused id numbers in case the object id number space gets exhausted (after adding some more lines, the one I suggested isn't sufficient to fix the thing), there goes something wrong in a subtle way still. The only thing I observed is that when I build a Super Tester Building, the effect never executes - the building remains, but the species isn't converted. So something doesn't work as intended.

I finally realized that reusing unused id numbers this way can't work reliably anyway. For that to work, you'd have to ensure that in the same atomic action that determines an unsued id, this id has to be used for an object, and that this object has to be inserted into the object map, because a subsequent call would yield the same id if that doesn't happen. That can't be done within this function.

Meaning, if I call that function several times because I want to create several objects, and after getting the id's and assigning them to my new objects I insert those objects, the result will be a big mess.

So I decided to just remove that loop entirely, as it can't serve it's proposed purpose anyway. I also thought it might be a good idea to log an error message should the object id number space ever get exhausted (which should be almost impossible).

Commit 1989c61, closing this issue.

from freeorion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.