Home World Forum
Stars! AutoHost web forums

Jump to Stars! AutoHost


 
 
Home » Stars! Clones, Extensions, Modding » Stars! Nova - Development » Need to move away from using names as IDs
Re: Need to move away from using names as IDs Wed, 29 June 2011 11:26 Go to previous messageGo to next message
Musmuris

 
Master Chief Petty Officer
Stars! Nova developer
Stars! Nova developer

Messages: 96
Registered: June 2011
Aeglos wrote on Wed, 29 June 2011 06:29

But what do we do when the counter runs out?

The standard Stars! limits are 16 ship designs and 10 starbase designs, while those are going to be configurable, they should default to Stars! values for balance reasons as stated on the wiki. And even if they don't, there is still a chance they may exhaust.

Although, we could just use those for Ids and track the total amounts with Count from their collections. Might be a better idea.



We can have over 16 million fleets/designs etc. They're not going to run out. Tracking how many there is easier than trying to re-use Ids. I was going to suggest adding a counter for each empire and ID type that just then increases each time

Report message to a moderator

Re: Need to move away from using names as IDs Wed, 29 June 2011 16:50 Go to previous messageGo to next message
Musmuris

 
Master Chief Petty Officer
Stars! Nova developer
Stars! Nova developer

Messages: 96
Registered: June 2011
Ok, I've removed "Key" from Item and added it explictly to all objects that use it. Now we can remove them one by one (I will start with Fleets)

I wasn't originaly thinking that Id would be on Item but I guess it can stay there. However in that case we definately should just have one counter per race for all item types (so all items will have a unique ID).

I like the way Owner now just uses the same ID field (I'd been thinking of separating them, but this also works!)

So I will work on Fleets now Very Happy

Report message to a moderator

Re: Need to move away from using names as IDs Wed, 29 June 2011 17:03 Go to previous messageGo to next message
Aeglos is currently offline Aeglos

 
Chief Warrant Officer 1
Stars! Nova developer
Stars! Nova developer

Messages: 142
Registered: May 2011
Location: Chile
I added a few properties to EmpireData for the counters. I'll work on implementing an EnemyIntel object to encapsulate enemyintel now.

I also changed the properties on Item; Id now returns the full itemId field. PureId now returns the Id without the owner bits.


[Updated on: Wed, 29 June 2011 17:04]

Report message to a moderator

Re: Need to move away from using names as IDs Fri, 01 July 2011 07:56 Go to previous messageGo to next message
Daniel is currently offline Daniel

 
Chief Warrant Officer 3
Stars! Nova developer
Stars! Nova developer

Messages: 179
Registered: April 2006
Location: Nowra, Australia
I have modified Item.Owner to bit shift values passed in or out so they are in the range 0 to 0x7F instead of being Item.Id & 0x7F000000. This allows code of the form item.Owner = 1 instead of item.Owner = (1 << 24); I also added some unit tests for item.Owner and item.PureId properties.

This fixes a couple of the integration tests for the battle engine, but there are more to go.



Have fun.

Report message to a moderator

Re: Need to move away from using names as IDs Fri, 01 July 2011 15:15 Go to previous messageGo to next message
Aeglos is currently offline Aeglos

 
Chief Warrant Officer 1
Stars! Nova developer
Stars! Nova developer

Messages: 142
Registered: May 2011
Location: Chile
That change breaks the game though, as EmpireData returns it's ID non-shifted (from 0x01000000 upwards) and Item gets out of range exceptions now. The idea was to use 0-127 empire ids on game setup and then operate solely on the higher bits for owner, so we don't mess between low values and very high values being equivalent elsewhere.

I'll change EmpireData to match this though, if it poses problems for the tests.

By the way, is anyone actually changing the fleets and designs to the new Ids? I can do it if not, I thought Morsen might have been on it so I haven't yet.

Report message to a moderator

Re: Need to move away from using names as IDs Sun, 03 July 2011 08:56 Go to previous messageGo to next message
Musmuris

 
Master Chief Petty Officer
Stars! Nova developer
Stars! Nova developer

Messages: 96
Registered: June 2011
I was also going to suggest just leaving the bits shifted up as well but no matter Smile

Report message to a moderator

Re: Need to move away from using names as IDs Sun, 03 July 2011 09:18 Go to previous messageGo to next message
Musmuris

 
Master Chief Petty Officer
Stars! Nova developer
Stars! Nova developer

Messages: 96
Registered: June 2011
Daniel wrote on Fri, 01 July 2011 07:56

I have modified Item.Owner to bit shift values passed in or out so they are in the range 0 to 0x7F instead of being Item.Id & 0x7F000000. This allows code of the form item.Owner = 1 instead of item.Owner = (1 << 24); I also added some unit tests for item.Owner and item.PureId properties.

This fixes a couple of the integration tests for the battle engine, but there are more to go.


It might fix the tests.... but it breaks the making of a new game! Laughing

So i've just reverted that change locally so I can carry on until we decide what to do

Report message to a moderator

Re: Need to move away from using names as IDs Sun, 03 July 2011 17:50 Go to previous messageGo to next message
Daniel is currently offline Daniel

 
Chief Warrant Officer 3
Stars! Nova developer
Stars! Nova developer

Messages: 179
Registered: April 2006
Location: Nowra, Australia
Musmuris wrote on Sun, 03 July 2011 22:56

I was also going to suggest just leaving the bits shifted up as well but no matter Smile


Leaving the bits shifted up results in strange behaviour at higher levels of abstraction. For example the combat test you create a fleet with a payer/empire id of 1 and then when you read it back you get 0x01000000. I think this behaviour is going to be very confusing. I will try to find a fix that allows the new game to be created.



Have fun.

Report message to a moderator

Re: Need to move away from using names as IDs Mon, 04 July 2011 03:25 Go to previous messageGo to next message
Aeglos is currently offline Aeglos

 
Chief Warrant Officer 1
Stars! Nova developer
Stars! Nova developer

Messages: 142
Registered: May 2011
Location: Chile
I haven't been able to work much this past days, should be able to do coding starting tomorrow again.

I was going to suggest changing the Owner property to accept both 0-127 and 0x01000000 to 0x7F000000 in the setter, but return only 0x01000000 and up on the getter. This allows to work well with both Empire.Id and with the unit tests, while using non-shifted high value ids regularly in the rest of the code.

Report message to a moderator

Re: Need to move away from using names as IDs Tue, 05 July 2011 17:52 Go to previous messageGo to next message
Musmuris

 
Master Chief Petty Officer
Stars! Nova developer
Stars! Nova developer

Messages: 96
Registered: June 2011
Aeglos wrote on Mon, 04 July 2011 03:25

I haven't been able to work much this past days, should be able to do coding starting tomorrow again.


No worries - I know the feeling Smile

You may want to take a copy of your code before updating... I may have create a merge or two for you. Sorry.

Report message to a moderator

Re: Need to move away from using names as IDs Wed, 06 July 2011 20:55 Go to previous messageGo to next message
Aeglos is currently offline Aeglos

 
Chief Warrant Officer 1
Stars! Nova developer
Stars! Nova developer

Messages: 142
Registered: May 2011
Location: Chile
The design refactoring is done; they are now tracked by Id.

This poses an interesting issue on game creation. On game initialization designs are preparated for each race, but they then need to be retrieved by Name as the meaning of each Id is lost quickly without the GUI aids.

I solved it by simply looping through all designs and pick by owner and name, as the collection is small at game creation, pending another clever solution.

EDIT: Forgot to mention: I removed the NextStarbaseDesign counter from EmpireData as it would have caused conflicts between normal and starbase designs on the collections. Both use the NextDesign counter only; since we have 16 millions ids, it should suffice.


[Updated on: Wed, 06 July 2011 23:55]

Report message to a moderator

Re: Need to move away from using names as IDs Thu, 07 July 2011 00:20 Go to previous messageGo to next message
Aeglos is currently offline Aeglos

 
Chief Warrant Officer 1
Stars! Nova developer
Stars! Nova developer

Messages: 142
Registered: May 2011
Location: Chile
Related to the Tests, I implemented a second property, <OwnerLow> on Item which operates on the range 1-127 on get and set, for use on the tests. The fleet constructor used in tests now uses it by default. Might make more sense to name it OwnerTest though...

Report message to a moderator

Re: Need to move away from using names as IDs Sat, 09 July 2011 03:15 Go to previous messageGo to next message
Daniel is currently offline Daniel

 
Chief Warrant Officer 3
Stars! Nova developer
Stars! Nova developer

Messages: 179
Registered: April 2006
Location: Nowra, Australia
Musmuris wrote on Sun, 03 July 2011 23:18

Daniel wrote on Fri, 01 July 2011 07:56

I have modified Item.Owner to bit shift values passed in or out so they are in the range 0 to 0x7F instead of being Item.Id & 0x7F000000. This allows code of the form item.Owner = 1 instead of item.Owner = (1 << 24); I also added some unit tests for item.Owner and item.PureId properties.

This fixes a couple of the integration tests for the battle engine, but there are more to go.


It might fix the tests.... but it breaks the making of a new game! Laughing

So i've just reverted that change locally so I can carry on until we decide what to do


Ok, so I have had another go at this. Firstly note that the current state of the code base (Rev 698) does not allow you to play turns. You can open and submit a turn, but turn generation is bugged due to the turn year issue discussed here. The code I will upload after posting this fixes this issue so you can submit your first turn properly. It however fails to generate, but that is a small step forward.

In Rev 698 we have 32 bit Ids comprising a sign bit, 7 owner bits and 24 PureId bits. The collection key is referred to as Id and setting and getting the owner breaks a number of unit tests

I have re-written the code to handle Ids. The backing store is now called key (accessed by the Key property) which is a signed 64 bit integer (aka long) thus:

        /// <summary>
        /// Backing store for the game wide unique key. 
        /// First bit is for sign. Negative values are reserved for special flags.
        /// Bits 2-24 are reserved.
        /// Bits 25-32 are for the empire.Id aka Owner.
        /// Bits 33-64 are the Item.Id, which is a number generated by the client and unique for objects owned by that empire.
        /// Bit map is:
        /// S-----------------------OOOOOOOOIIIIIIIIIIIIIIIIIIIIIIIIIIII IIII
        /// IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
        /// ^                  ^                ^            ^
        /// +-- sign bit       +-- reserved     +-- owner    +-- client generated Id
        /// </summary>
        private long key = Global.NoOwner; // Default to no-id and no owner.


As you can see there is still a sign bit (not really used). The owner is now an unsigned 8 bit number (ushort) from 1 to 255 (zero reserved for unknown/unset). The Id (formerly the PureId) is a 32 bit unsigned number (uint), again zero is for unknown/unset.

Item also implements Owner and Id properties to access the masked fields embedded in the Key. These should always be used when referring to just the Owner (or empire.Id); or to the Id portion of the key. The Id is unique to the client but not necessarily unique in the game and should be only used for generating display values such as in a fleet name.

This has been a big change and has taken two days to track down the bugs caused by miss matched Ids and Keys. Using long, uint and ushort was a strategy to try and catch some of these bugs, however implicit casting still causes some problems.

The advantage is now that a collection.Key is always generated from an Item.Key, an owner/empire.Id is always a number from 1-255 and there is no need to think about bit masking or shifting outside of the Item object which implements this.

I will now try to fix the rest of the turn generation issues caused by these changes.



Have fun.

Report message to a moderator

Re: Need to move away from using names as IDs Sat, 09 July 2011 04:06 Go to previous messageGo to next message
Aeglos is currently offline Aeglos

 
Chief Warrant Officer 1
Stars! Nova developer
Stars! Nova developer

Messages: 142
Registered: May 2011
Location: Chile
Sounds good to me; it's like the finishing touches that consolidate the system.

I assume the 2-24 reserved bits are futureproofing for flags?

One thing though; last time I tried to use unsigned values (for Resources), I had a heck of an ugly time and needed a tremendous amount of casts to make it work. I asume this wouldn't be the case as Ids/Keys are fairly isolated from the rest of the code as far as setting and getting goes.

Also and just for the record, unisnged types seem to be non conformant with the CLS spec. We shouldn't have any issue though as we are not interoperating across languages.

Report message to a moderator

Re: Need to move away from using names as IDs Sat, 09 July 2011 04:37 Go to previous messageGo to next message
Daniel is currently offline Daniel

 
Chief Warrant Officer 3
Stars! Nova developer
Stars! Nova developer

Messages: 179
Registered: April 2006
Location: Nowra, Australia
Aeglos wrote on Sat, 09 July 2011 18:06

Sounds good to me; it's like the finishing touches that consolidate the system.

I assume the 2-24 reserved bits are futureproofing for flags?



They are so the Id could be a full uint and I could add a seperate value for owner, as this allowed me to use different types for Id and owner to catch some of the casting bugs.

Aeglos wrote on Sat, 09 July 2011 18:06


One thing though; last time I tried to use unsigned values (for Resources), I had a heck of an ugly time and needed a tremendous amount of casts to make it work. I asume this wouldn't be the case as Ids/Keys are fairly isolated from the rest of the code as far as setting and getting goes.


It is all handled inside Item. If you are casting with owner or Id (except to/from strings) you are probably doing something wrong.

Aeglos wrote on Sat, 09 July 2011 18:06


Also and just for the record, unisnged types seem to be non conformant with the CLS spec. We shouldn't have any issue though as we are not interoperating across languages.


Possibly an issue for third party tools. I tried to create a type definition for Id and owner so it would be easier to change the type in the future if required, like:

typedef owner_t ushort;


However this is not supported in C#. I would love to have the sort of type control available in ADA for this as it would save me hunting down a lot of runtime issues caused by implicit casts that should be errors, usually where an Id is used instead of a Key.



Have fun.

Report message to a moderator

Re: Need to move away from using names as IDs Sat, 09 July 2011 07:48 Go to previous message
Daniel is currently offline Daniel

 
Chief Warrant Officer 3
Stars! Nova developer
Stars! Nova developer

Messages: 179
Registered: April 2006
Location: Nowra, Australia
Turns are now generating again, though I have not played a game through - can build factories, mines and defenses. Fixed disappearing home world caused by copying StarReports for unexplored stars over the ServerState in two player (human vs ai) game.


Have fun.

Report message to a moderator

Previous Topic: Turn year
Next Topic: Orders overwriting Star Data
Goto Forum:
  


Current Time: Thu Mar 28 12:50:06 EDT 2024