It reminds me of a little starry: D
I would simply add an integer to the building's queue, which represents the time when it is busy. Of course, you need to update this variable once in a while. (Temporary units are here "here", in seconds)
So, let's say we have a building, and we send 3 units, each of which takes 5 seconds. Which summarizes up to 15 s. We are in time = 0. Then we have another building in which we send 2 units, which require 6 timeouts to complete each of them.
So we can have a table like this:
Time 0 Building 1, 3 units, 15s to complete. Building 2, 2 units, 12s to complete. Time 1 Building 1, 3 units, 14s to complete. Building 2, 2 units, 12s to complete.
And we want to add another block that takes 2 seconds, we can just skip the selected buildings and choose the one with the lowest time to complete. In this case, this will be creation 2. This will result in Time2 ...
Time 2 Building 1, 3 units, 13s to complete Building 2, 3 units, 11s+2s=13s to complete
...
Time 5 Building 1, 2 units, 10s to complete (5s are over, the first unit pops out) Building 2, 3 units, 10s to complete
Etc.
Of course, you must take care of the upper bounds at your production facilities. For example, if a building has 5 elements, do not assign something and choose the next building, which has the lowest time to complete.
I donβt know if you can easily implement this using your engine or even support some kind of temporary units.
This simply leads to updating all production facilities once per time, O (n), where n is the number of buildings that can create something. If you send the device, it will take O (1), assuming you save the selected buildings in sorted order, the lowest at first - so just search for the first item. In this case, you need to resort to the list after manipulating units, for example, cancel or add.
Otherwise, the answer also seems possible.